Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

perf: stream prune output as integration checks complete (#2015)

Maximilian Roosc6fb4d4f48e5
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rs
index cb721f723..e2e1a8c4b 100644
--- a/src/commands/step_commands.rs
+++ b/src/commands/step_commands.rs
@@ -1379,7 +1379,7 @@ pub fn step_prune(
approved
};
- let mut candidates: Vec<Candidate> = Vec::new();
+ let mut removed: 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
@@ -1505,12 +1505,14 @@ pub fn step_prune(
});
}
- // Phase 1: Parallel integration checks with streaming results.
+ // Parallel integration checks with inline removals.
//
// 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.
+ // processes results as they arrive: age-filtering, printing "Skipped"
+ // messages, and removing candidates immediately. This overlaps integration
+ // checking with removal — output appears as soon as the first check
+ // completes instead of waiting for all checks to finish.
let (tx, rx) = chan::unbounded();
let integration_refs: Vec<String> = check_items
.iter()
@@ -1573,6 +1575,12 @@ pub fn step_prune(
{
let age = Duration::from_secs(now_secs.saturating_sub(created_epoch.as_secs()));
if age < min_age_duration {
+ if !dry_run {
+ eprintln!(
+ "{}",
+ info_message(format!("Skipped {label} (younger than {min_age})"))
+ );
+ }
skipped_young.push(label);
continue;
}
@@ -1601,8 +1609,8 @@ pub fn step_prune(
dry_run_info.push((candidate, info));
} else if is_current {
deferred_current = Some(candidate);
- } else {
- candidates.push(candidate);
+ } else if try_remove(&candidate, &repo, &config, foreground, run_hooks)? {
+ removed.push(candidate);
}
continue;
}
@@ -1626,6 +1634,12 @@ pub fn step_prune(
{
let age = Duration::from_secs(now_secs.saturating_sub(created_epoch));
if age < min_age_duration {
+ if !dry_run {
+ eprintln!(
+ "{}",
+ info_message(format!("Skipped {branch} (younger than {min_age})"))
+ );
+ }
skipped_young.push(branch.clone());
continue;
}
@@ -1646,8 +1660,8 @@ pub fn step_prune(
suffix,
};
dry_run_info.push((candidate, info));
- } else {
- candidates.push(candidate);
+ } else if try_remove(&candidate, &repo, &config, foreground, run_hooks)? {
+ removed.push(candidate);
}
}
@@ -1688,7 +1702,9 @@ pub fn step_prune(
dry_candidates.push(candidate);
}
- // Report skipped worktrees (after candidates, before summary)
+ // Report skipped worktrees (after candidates, before summary).
+ // Sort for deterministic output regardless of channel completion order.
+ skipped_young.sort();
if !skipped_young.is_empty() {
let names = skipped_young.join(", ");
eprintln!(
@@ -1713,42 +1729,6 @@ pub fn step_prune(
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(&current, &repo, &config, foreground, run_hooks)?
diff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rs
index 364f3b7d5..37e47dd82 100644
--- a/tests/integration_tests/step_prune.rs
+++ b/tests/integration_tests/step_prune.rs
@@ -501,13 +501,13 @@ fn test_prune_stale_plus_young(mut repo: TestRepo) {
// 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 a candidate
- assert_cmd_snapshot!(make_snapshot_cmd(
- &repo,
- "step",
- &["prune", "--dry-run"],
- None
- ));
+ // Orphan branch (no worktree) at HEAD: integrated but appears young
+ repo.create_branch("young-orphan");
+
+ // Epoch 30 minutes after GIT_COMMITTER_DATE → orphan branch appears 30min old, < 1h
+ let mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--dry-run"], None);
+ cmd.env("WORKTRUNK_TEST_EPOCH", "1735691400");
+ assert_cmd_snapshot!(cmd);
}
/// Non-dry-run variant of `test_prune_stale_plus_young`: exercises the skipped_young
@@ -524,7 +524,9 @@ fn test_prune_stale_plus_young_non_dry_run(mut repo: TestRepo) {
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));
+ let mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--yes"], None);
+ cmd.env("RAYON_NUM_THREADS", "1"); // deterministic output order
+ assert_cmd_snapshot!(cmd);
}
/// Prune detects squash-merged branches when target later modified the same files (#1818).
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young.snap
index 68f8d18cb..97219d7f8 100644
--- a/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young.snap
+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_stale_plus_young.snap
@@ -33,7 +33,7 @@ info:
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
- WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_EPOCH: "1735691400"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
@@ -46,5 +46,5 @@ exit_code: 0
----- stderr -----
○ stale-branch (stale) — same commit as main
-○ Skipped young-branch (younger than 1h)
+○ Skipped young-branch, young-orphan (younger than 1h)
↳ 1 branch would be removed (dry run)
diff --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.snap
index f24b8b106..e7e5e873f 100644
--- 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.snap
@@ -24,6 +24,7 @@ info:
OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
PATH: "[PATH]"
PSModulePath: ""
+ RAYON_NUM_THREADS: "1"
RUST_LOG: warn
SHELL: ""
TERM: alacritty
@@ -45,6 +46,6 @@ exit_code: 0
----- stdout -----
----- stderr -----
-○ Skipped young-branch (younger than 1h)
✓ Pruned stale worktree & removed branch stale-branch (same commit as main, _)
+○ Skipped young-branch (younger than 1h)
✓ Pruned 1 branch