Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(picker): stop first-keystroke freeze (#3087)
Ben Druckerf334794fb65b
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rsindex db3b005fd..f92236b6d 100644--- a/src/commands/list/collect/mod.rs+++ b/src/commands/list/collect/mod.rs@@ -282,6 +282,7 @@ mod types;use anyhow::Context;use std::collections::HashSet;use std::sync::Arc;+use std::sync::LazyLock;use anstyle::Style;use color_print::cformat;@@ -311,6 +312,44 @@ use results::drain_results;use types::DrainOutcome;use types::{MissingResult, TaskError, TaskResult};+/// Dedicated rayon pool for git-heavy worktree collection and preview+/// pre-compute, kept off the global pool.+///+/// The picker (skim) runs its per-keystroke fuzzy matcher and result sort on+/// the **global** rayon pool. Worktrunk's collection floods that same pool with+/// blocking git subprocess tasks (status, diff, rev-list, merge-base…), one+/// batch per worktree, plus the preview orchestrator's per-mode `git diff` /+/// `git log` tasks. With only `2× CPU` global workers, all blocked on+/// subprocesses, skim's matcher queues behind the flood and the picker freezes+/// for seconds on the first keystroke, scaling with worktree count.+///+/// Routing collection and preview work through this pool leaves the global pool+/// free for skim. Same isolation pattern as `copy::COPY_POOL` and+/// `remove_dir::REMOVE_POOL`, but sized like the global pool+/// ([`collect_pool_num_threads`], i.e. `2× CPU`, honoring `RAYON_NUM_THREADS`)+/// so collection throughput is unchanged. The goal is separation, not a+/// smaller pool.+pub(crate) static COLLECT_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {+ let num_threads = collect_pool_num_threads(std::env::var_os("RAYON_NUM_THREADS").is_some());+ rayon::ThreadPoolBuilder::new()+ .num_threads(num_threads)+ .build()+ .expect("failed to build collect thread pool")+});++/// The `num_threads` argument for [`COLLECT_POOL`]. When `RAYON_NUM_THREADS`+/// is set, return 0 so Rayon reads and validates the env var itself.+/// Otherwise return [`crate::rayon_thread_count`] (`2× CPU`), since Rayon's+/// own default is `1× CPU`. Takes the env presence as a parameter so both+/// branches are unit-testable without mutating process-global environment.+fn collect_pool_num_threads(rayon_num_threads_set: bool) -> usize {+ if rayon_num_threads_set {+ 0+ } else {+ crate::rayon_thread_count()+ }+}+struct TableRenderPlan {progressive_table: Option<ProgressiveTable>,header: String,@@ -455,8 +494,8 @@ pub trait PickerProgressHandler: Send + Sync {/// Fired once before `collect` returns `Ok(Some(_))`. Lets the picker/// kick off the deferred tier of background work — secondary preview- /// modes for items 1..N, and LLM summaries for items 1..N. The- /// global rayon pool serves both pipelines; deferring this tier+ /// modes for items 1..N, and LLM summaries for items 1..N.+ /// `COLLECT_POOL` serves both pipelines. Deferring this tier/// until drain-end keeps low-priority preview submissions out of/// the injector while row tasks dominate worker deques.///@@ -1455,10 +1494,15 @@ pub fn collect(worktrunk::trace::instant("Spawning worker thread");std::thread::spawn(move || {worktrunk::trace::instant("Parallel execution started");- all_work_items.into_par_iter().for_each(|item| {- worktrunk::shell_exec::set_command_timeout(command_timeout);- let result = item.execute();- let _ = tx_worker.send(result);+ // Run on the dedicated `COLLECT_POOL` so the blocking git subprocess+ // tasks leave the global pool free for skim's per-keystroke matcher+ // when the picker is open. See `COLLECT_POOL`.+ COLLECT_POOL.install(|| {+ all_work_items.into_par_iter().for_each(|item| {+ worktrunk::shell_exec::set_command_timeout(command_timeout);+ let result = item.execute();+ let _ = tx_worker.send(result);+ });});});@@ -1950,11 +1994,15 @@ pub fn populate_item(// Sort: network tasks lastwork_items.sort_by_key(|w| w.kind.is_network());- // Spawn collection in background thread (executes only)+ // Spawn collection in background thread (executes only). Route through+ // `COLLECT_POOL` for consistency with the multi-item path, though a single+ // item never floods the global pool.std::thread::spawn(move || {- work_items.into_par_iter().for_each(|w| {- let result = w.execute();- let _ = tx.send(result);+ COLLECT_POOL.install(|| {+ work_items.into_par_iter().for_each(|w| {+ let result = w.execute();+ let _ = tx.send(result);+ });});});@@ -2018,6 +2066,14 @@ mod tests {use super::*;use ansi_str::AnsiStr;+ #[test]+ fn test_collect_pool_num_threads_honors_env() {+ // Env var set: defer to Rayon (0 means "read RAYON_NUM_THREADS").+ assert_eq!(collect_pool_num_threads(true), 0);+ // Env var unset: explicit 2× CPU, since Rayon's own default is 1×.+ assert_eq!(collect_pool_num_threads(false), crate::rayon_thread_count());+ }+#[test]fn test_format_stall_footer_single_pending() {let rendered =diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex 9cb7cc2de..c841c2a4d 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -23,7 +23,7 @@//! 1. `current_or_recover` + config resolution.//! 2. `PreviewState::new` — auto-detects Right vs Down layout.//! 3. Allocates the `PreviewOrchestrator` and kicks off a *speculative*-//! `git diff HEAD` for the current worktree on the global rayon pool.+//! `git diff HEAD` for the current worktree on `COLLECT_POOL`.//! That bg work overlaps with everything below.//! 4. Computes `num_items_estimate` — `list_worktrees` plus (conditionally)//! `local_branches` / `remote_branches`, capped at@@ -486,7 +486,7 @@ pub fn handle_picker(// Preview cache is created up-front so the speculative first-item// preview can run in parallel with `collect::collect` below. Tasks- // route to the global rayon pool (shared with the row pipeline).+ // route to `COLLECT_POOL` (shared with the row pipeline).// Wrapped in `Arc` because the progressive handler (running on the// collect background thread) also calls `spawn_preview`.//@@ -805,7 +805,7 @@ pub fn handle_picker(// Dry-run / preview-bench: skim is bypassed. Wait for collect (which// spawns previews via the handler) to finish, then for the orchestrator's- // pending tasks to drain on the global rayon pool. Dry-run additionally+ // pending tasks to drain on `COLLECT_POOL`. Dry-run additionally// drains stashed warnings and dumps the cache inventory; preview-bench// returns immediately so the measured wall clock is just "spawn → all// preview tasks drained", with no JSON serialization or stderr I/O indiff --git a/src/commands/picker/preview_cache.rs b/src/commands/picker/preview_cache.rsindex e89f72eaf..548594363 100644--- a/src/commands/picker/preview_cache.rs+++ b/src/commands/picker/preview_cache.rs@@ -56,7 +56,7 @@ const KIND: &str = "picker-preview";/// commonly: a squash merge landing `main` at the cached SHA) would/// leave the decoration text stale even though the cache key is still/// valid. The orchestrator mitigates this by enqueuing a background-/// refresh task on the global rayon pool whenever a Log preview hit+/// refresh task on `COLLECT_POOL` whenever a Log preview hit/// the disk cache; the refresh re-runs `compute_log_raw_and_stats`,/// overwrites this entry, and updates the in-memory `PreviewCache`./// The cached entry served on the *current* render is still potentiallydiff --git a/src/commands/picker/preview_orchestrator.rs b/src/commands/picker/preview_orchestrator.rsindex d097b9375..68ed94611 100644--- a/src/commands/picker/preview_orchestrator.rs+++ b/src/commands/picker/preview_orchestrator.rs@@ -1,10 +1,15 @@//! Background preview pre-compute orchestration.//!-//! Routes preview tasks to the global rayon pool (shared with the row-//! pipeline) and tracks the in-memory cache. A single pool lets workers+//! Routes preview tasks to the dedicated [`COLLECT_POOL`] (shared with the+//! row pipeline) and tracks the in-memory cache. A single pool lets workers//! prefer whichever workload has dominant pressure: row tasks land on//! workers' local deques and take priority during drain; preview tasks-//! sit in the global injector and pick up workers as they free.+//! sit in the pool's injector and pick up workers as they free.+//!+//! `COLLECT_POOL` is deliberately *not* the global pool: skim runs its+//! per-keystroke fuzzy matcher on the global pool, so keeping these blocking+//! git tasks off it is what prevents the picker from freezing on the first+//! keystroke. See [`COLLECT_POOL`] for the full reasoning.//!//! Provides a pending-task counter for the dry-run path//! (`WORKTRUNK_PICKER_DRY_RUN`) and tests, both of which want to wait for@@ -21,6 +26,7 @@ use worktrunk::git::Repository;use super::items::{PreviewCache, WorktreeSkimItem};use super::preview::PreviewMode;use super::summary;+use crate::commands::list::collect::COLLECT_POOL;use crate::commands::list::model::ListItem;/// The picker's initial preview tab — `WorkingTree`, shown when the@@ -82,8 +88,9 @@ impl PreviewOrchestrator {////// Log mode that hits the disk cache also enqueues a refresh task/// (via `rayon::spawn_fifo`, so it lands behind in-flight foreground- /// precompute submitted with `rayon::spawn`) to recompute the- /// embedded ref decorations before the next visit. See the+ /// precompute) to recompute the embedded ref decorations before the+ /// next visit. The `spawn_fifo` runs from inside a `COLLECT_POOL`+ /// worker, so it inherits that pool rather than the global one. See the/// `LogCacheEntry` docstring for why the disk cache itself is/// SHA-keyed but decoration text drifts.pub(super) fn spawn_preview(@@ -170,10 +177,10 @@ impl PreviewOrchestrator {/// Spawn the deferred pre-compute tier for items 1..N.////// Fires from the picker handler's `on_collect_complete` hook — i.e.- /// after `collect::collect`'s drain ends. The global rayon pool- /// serves both the row pipeline and the preview pipeline; deferring- /// this tier keeps these submissions out of the global injector- /// while row tasks are still landing on workers' local deques. The+ /// after `collect::collect`'s drain ends. `COLLECT_POOL` serves+ /// both the row pipeline and the preview pipeline. Deferring this+ /// tier keeps these submissions out of that pool's injector while+ /// row tasks are still landing on workers' local deques. The/// default tab for these rows already fired at skeleton time via/// [`Self::spawn_initial_precompute`]; what's left is/// [`SECONDARY_MODES`] plus summaries.@@ -227,10 +234,10 @@ impl PreviewOrchestrator {task();};// The `pending` counter is independent of which pool the task- // lands on, so routing through `rayon::spawn` (global pool, shared- // with the row pipeline) doesn't change `wait_for_idle` semantics- // in tests or the dry-run path.- rayon::spawn(wrapped);+ // lands on, so routing through `COLLECT_POOL` (shared with the row+ // pipeline, off the global pool skim's matcher uses) doesn't change+ // `wait_for_idle` semantics in tests or the dry-run path.+ COLLECT_POOL.spawn(wrapped);}/// Block until all spawned tasks complete.diff --git a/src/commands/picker/progressive_handler.rs b/src/commands/picker/progressive_handler.rsindex 0d3eaa240..259cae6e2 100644--- a/src/commands/picker/progressive_handler.rs+++ b/src/commands/picker/progressive_handler.rs@@ -12,9 +12,9 @@//! every row when summaries are disabled.//! - `on_collect_complete` fires the secondary modes (Log / BranchDiff ///! UpstreamDiff) and summaries for items 1..N once the row pipeline-//! has torn down. Preview tasks share the global rayon pool with the-//! row pipeline; staging keeps low-priority preview submissions out-//! of the global injector while row tasks are still landing on+//! has torn down. Preview tasks share `COLLECT_POOL` with the row+//! pipeline. Staging keeps low-priority preview submissions out of+//! that pool's injector while row tasks are still landing on//! workers' local deques during drain.use std::sync::{Arc, Mutex, OnceLock};@@ -120,7 +120,7 @@ impl PickerProgressHandler for PickerHandler {// other row's default tab. Tier 2 (secondary modes + summaries// for items 1..N) fires from `on_collect_complete` after the row// pipeline tears down — spawning that bulk now would queue ahead- // of row tasks in the global injector while workers are still+ // of row tasks in `COLLECT_POOL`'s injector while workers are still// grinding through the row work.self.orchestrator.spawn_initial_precompute(&list_items,