Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
perf: stream prune integration checks, parallel removals (#1950)
Maximilian Roosda5a3905aea5
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex f7227a3a7..602477dfc 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -19,6 +19,7 @@ use std::time::Duration;use anyhow::Context;use color_print::cformat;+use crossbeam_channel as chan;use ignore::gitignore::GitignoreBuilder;use rayon::prelude::*;use worktrunk::HookType;@@ -1277,6 +1278,8 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->// Gather candidates: integrated worktrees + integrated branch-only refsstruct Candidate {+ /// Original index in check_items (for deterministic output ordering)+ check_idx: usize,/// Branch name (None for detached HEAD worktrees)branch: Option<String>,/// Display label: branch name or abbreviated commit SHA@@ -1359,9 +1362,8 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->approved};- let mut candidates: Vec<Candidate> = Vec::new(); // dry-run collects here- let mut removed: Vec<Candidate> = Vec::new(); // non-dry-run tracks removals- let mut deferred_current: Option<Candidate> = None; // current worktree removed last+ let mut candidates: Vec<Candidate> = Vec::new();+ let mut deferred_current: Option<Candidate> = None;let mut skipped_young: Vec<String> = Vec::new();// Track branches seen via worktree entries so we don't double-count// in the orphan branch scan below.@@ -1486,22 +1488,51 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->});}- let integration_results: Vec<anyhow::Result<_>> = check_items- .par_iter()- .map(|item| {- let (effective_target, reason) =- repo.integration_reason(&item.integration_ref, &integration_target)?;- Ok((effective_target, reason))- })+ // Phase 1: Parallel integration checks with streaming results.+ //+ // Spawn integration checks on a background thread via rayon par_iter,+ // sending each result through a channel as it completes. The main thread+ // processes results as they arrive for age filtering and candidate+ // collection, overlapping with still-running checks.+ let (tx, rx) = chan::unbounded();+ let integration_refs: Vec<String> = check_items+ .iter()+ .map(|item| item.integration_ref.clone()).collect();- // Process results sequentially (removals must be serial)- for (item, result) in check_items.iter().zip(integration_results) {+ // Intentionally detached: if the main thread returns early (error in+ // the recv loop), remaining rayon tasks silently fail to send on the+ // closed channel and the thread cleans up on its own. Empty+ // integration_refs produces an empty par_iter that completes immediately.+ let repo_clone = repo.clone();+ let target = integration_target.clone();+ std::thread::spawn(move || {+ integration_refs+ .into_par_iter()+ .enumerate()+ .for_each(|(idx, ref_name)| {+ let result = repo_clone.integration_reason(&ref_name, &target);+ let _ = tx.send((idx, result));+ });+ });++ // Collect integration context alongside candidates for dry-run display.+ struct DryRunInfo {+ reason_desc: String,+ effective_target: String,+ suffix: &'static str,+ }+ let mut dry_run_info: Vec<(Candidate, DryRunInfo)> = Vec::new();++ // Process results as they arrive from the channel.+ for (idx, result) in rx {let (effective_target, reason) = result?;let Some(reason) = reason else {continue;};+ let item = &check_items[idx];+// Linked worktrees need special handling: age check via filesystem// metadata, current-worktree deferral, and path-based candidates.if let CheckSource::Linked { wt_idx } = &item.source {@@ -1534,6 +1565,7 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->let wt_path = dunce::canonicalize(&wt.path).unwrap_or(wt.path.clone());let is_current = wt_path == current_root;let candidate = Candidate {+ check_idx: idx,branch: if wt.detached { None } else { wt.branch.clone() },label,path: Some(wt.path.clone()),@@ -1544,20 +1576,16 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->},};if dry_run {- eprintln!(- "{}",- info_message(cformat!(- "<bold>{}</> — {} {}",- candidate.label,- reason.description(),- effective_target- ))- );- candidates.push(candidate);+ let info = DryRunInfo {+ reason_desc: reason.description().to_string(),+ effective_target,+ suffix: "",+ };+ dry_run_info.push((candidate, info));} else if is_current {deferred_current = Some(candidate);- } else if try_remove(&candidate, &repo, &config, foreground, run_hooks)? {- removed.push(candidate);+ } else {+ candidates.push(candidate);}continue;}@@ -1588,39 +1616,53 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->}let candidate = Candidate {+ check_idx: idx,label: branch.clone(),branch: Some(branch.clone()),path: None,kind: CandidateKind::BranchOnly,};if dry_run {+ let info = DryRunInfo {+ reason_desc: reason.description().to_string(),+ effective_target,+ suffix,+ };+ dry_run_info.push((candidate, info));+ } else {+ candidates.push(candidate);+ }+ }++ if dry_run {+ // Sort by original check order for deterministic output regardless of+ // channel completion order.+ dry_run_info.sort_by_key(|(c, _)| c.check_idx);+ let mut dry_candidates = Vec::new();+ for (candidate, info) in dry_run_info {eprintln!("{}",info_message(cformat!("<bold>{}</>{} — {} {}",- branch,- suffix,- reason.description(),- effective_target+ candidate.label,+ info.suffix,+ info.reason_desc,+ info.effective_target)));- candidates.push(candidate);- } else if try_remove(&candidate, &repo, &config, foreground, run_hooks)? {- removed.push(candidate);+ dry_candidates.push(candidate);}- }- // Report skipped worktrees- if !skipped_young.is_empty() {- let names = skipped_young.join(", ");- eprintln!(- "{}",- info_message(format!("Skipped {names} (younger than {min_age})"))- );- }+ // Report skipped worktrees (after candidates, before summary)+ if !skipped_young.is_empty() {+ let names = skipped_young.join(", ");+ eprintln!(+ "{}",+ info_message(format!("Skipped {names} (younger than {min_age})"))+ );+ }- if dry_run {- if candidates.is_empty() {+ if dry_candidates.is_empty() {if skipped_young.is_empty() {eprintln!("{}", info_message("No merged worktrees to remove"));}@@ -1630,12 +1672,48 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->"{}",hint_message(format!("{} would be removed (dry run)",- prune_summary(&candidates)+ prune_summary(&dry_candidates))));return Ok(());}+ // Report skipped worktrees+ if !skipped_young.is_empty() {+ let names = skipped_young.join(", ");+ eprintln!(+ "{}",+ info_message(format!("Skipped {names} (younger than {min_age})"))+ );+ }++ // Phase 2: Parallel batch removal of confirmed candidates.+ //+ // Sort by original check order so output is deterministic when rayon+ // happens to process items sequentially (small candidate counts). With+ // many candidates, printed output from handle_remove_output may arrive+ // in completion order — each eprintln! is line-atomic so messages don't+ // interleave within a line.+ candidates.sort_by_key(|c| c.check_idx);+ let mut removed: Vec<Candidate> = if candidates.is_empty() {+ Vec::new()+ } else {+ let results: Vec<(Candidate, anyhow::Result<bool>)> = candidates+ .into_par_iter()+ .map(|candidate| {+ let result = try_remove(&candidate, &repo, &config, foreground, run_hooks);+ (candidate, result)+ })+ .collect();+ let mut removed = Vec::new();+ for (candidate, result) in results {+ if result? {+ removed.push(candidate);+ }+ }+ removed+ };+// Remove deferred current worktree last (cd-to-primary happens here)if let Some(current) = deferred_current&& try_remove(¤t, &repo, &config, foreground, run_hooks)?diff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rsindex f24299e24..064850c7c 100644--- a/tests/integration_tests/step_prune.rs+++ b/tests/integration_tests/step_prune.rs@@ -140,12 +140,9 @@ fn test_prune_multiple(mut repo: TestRepo) {repo.add_worktree("merged-b");repo.add_worktree("merged-c");- assert_cmd_snapshot!(make_snapshot_cmd(- &repo,- "step",- &["prune", "--yes", "--min-age=0s"],- None- ));+ let mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--yes", "--min-age=0s"], None);+ cmd.env("RAYON_NUM_THREADS", "1"); // deterministic output order+ assert_cmd_snapshot!(cmd);// All merged worktrees removed (non-current — no placeholders)let parent = repo.root_path().parent().unwrap();@@ -223,12 +220,9 @@ fn test_prune_removes_multiple_detached(mut repo: TestRepo) {repo.add_worktree("detached-b");repo.detach_head_in_worktree("detached-b");- assert_cmd_snapshot!(make_snapshot_cmd(- &repo,- "step",- &["prune", "--yes", "--min-age=0s"],- None- ));+ let mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--yes", "--min-age=0s"], None);+ cmd.env("RAYON_NUM_THREADS", "1"); // deterministic output order+ assert_cmd_snapshot!(cmd);let parent = repo.root_path().parent().unwrap();assert!(@@ -291,6 +285,7 @@ fn test_prune_orphan_branches(mut repo: TestRepo) {// Far-future epoch: branches appear ~5 years old, passing the default 1h guardlet mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--yes"], None);cmd.env("WORKTRUNK_TEST_EPOCH", "1893456000"); // 2030-01-01+ cmd.env("RAYON_NUM_THREADS", "1"); // deterministic output orderassert_cmd_snapshot!(cmd);}@@ -325,12 +320,9 @@ fn test_prune_mixed_worktree_and_orphan_branch(mut repo: TestRepo) {// Worktree candidate: integrated worktree at the same commit as main.repo.add_worktree("merged-mixed");- assert_cmd_snapshot!(make_snapshot_cmd(- &repo,- "step",- &["prune", "--yes", "--min-age=0s"],- None- ));+ let mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--yes", "--min-age=0s"], None);+ cmd.env("RAYON_NUM_THREADS", "1"); // deterministic output order+ assert_cmd_snapshot!(cmd);let parent = repo.root_path().parent().unwrap();assert!(@@ -515,6 +507,23 @@ fn test_prune_stale_plus_young(mut repo: TestRepo) {));}+/// Non-dry-run variant of `test_prune_stale_plus_young`: exercises the skipped_young+/// message in the non-dry-run removal path.+#[rstest]+fn test_prune_stale_plus_young_non_dry_run(mut repo: TestRepo) {+ repo.commit("initial");++ // Stale worktree: directory deleted, but git metadata remains → candidate+ let wt_path = repo.add_worktree("stale-branch");+ std::fs::remove_dir_all(&wt_path).unwrap();++ // Regular merged worktree: with default epoch it appears "young"+ repo.add_worktree("young-branch");++ // Default min-age (1h) — young-branch is skipped, stale-branch is removed+ assert_cmd_snapshot!(make_snapshot_cmd(&repo, "step", &["prune", "--yes"], None));+}+/// Prune detects squash-merged branches when target later modified the same files (#1818).////// When `git merge-tree --write-tree` conflicts because the branch and target bothdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young_non_dry_run.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young_non_dry_run.snapnew file mode 100644index 000000000..f24b8b106--- /dev/null+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young_non_dry_run.snap@@ -0,0 +1,50 @@+---+source: tests/integration_tests/step_prune.rs+info:+ program: wt+ args:+ - step+ - prune+ - "--yes"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""+ OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"+ PATH: "[PATH]"+ PSModulePath: ""+ RUST_LOG: warn+ SHELL: ""+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"+ WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"+ WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[2m○[22m Skipped young-branch (younger than 1h)+[32m✓ Pruned stale worktree & removed branch [1mstale-branch[22m (same commit as [1mmain[22m,[39m [2m_[22m[32m)[39m+[32m✓[39m [32mPruned 1 branch[39m