Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor(wt-perf): cache fixtures in the platform cache dir, not /tmp or target/ (#3542)
Maximilian Roos4121dc979c2c
diff --git a/.github/workflows/benchmarks.yaml b/.github/workflows/benchmarks.yamlindex 3710f0f11..ba8a49016 100644--- a/.github/workflows/benchmarks.yaml+++ b/.github/workflows/benchmarks.yaml@@ -29,6 +29,10 @@ env:CARGO_TERM_COLOR: alwaysCARGO_INCREMENTAL: 0RUSTFLAGS: -C debuginfo=0+ # Pin wt-perf's fixture cache to an explicit path so the `actions/cache` step+ # below and wt-perf itself can't drift (wt-perf would otherwise compute the+ # root from the XDG cache dir, which honors $XDG_CACHE_HOME).+ WT_PERF_CACHE_DIR: ${{ github.workspace }}/.wt-perf-cachejobs:benchmarks:@@ -59,7 +63,10 @@ jobs:- name: π° Rust repo cacheuses: actions/cache@v6with:- path: target/bench-repos+ # wt-perf caches real-repo fixtures under WT_PERF_CACHE_DIR (set above),+ # not target/, so they survive `cargo clean`. Referencing the same var+ # here keeps the cached path and the path wt-perf writes in lockstep.+ path: ${{ env.WT_PERF_CACHE_DIR }}/bench-reposkey: bench-repos-rust-${{ runner.os }}- name: π Run benchmarksdiff --git a/Cargo.lock b/Cargo.lockindex 9145f6043..c7db7765d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4598,6 +4598,7 @@ dependencies = ["clap","criterion","dunce",+ "etcetera","serde_json","tempfile","worktrunk",diff --git a/Cargo.toml b/Cargo.tomlindex 9dce3cfdf..82e659cda 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -102,8 +102,8 @@ syntax-highlighting = ["dep:tree-sitter", "dep:tree-sitter-bash", "dep:tree-sitt# Includes: shell wrapper tests, PTY-based approval prompts, TUI select, progressive renderingshell-integration-tests = []# Enable benchmarks whose fixture is too large for hosted CI: the rust-scale-# prune fixture (~15 GiB of rust-lang/rust worktrees, cached in-# target/bench-repos/). The daily benchmarks workflow runs plain `cargo bench`+# prune fixture (~15 GiB of rust-lang/rust worktrees, cached in the wt-perf+# cache dir). The daily benchmarks workflow runs plain `cargo bench`# and must never build it; opt in locally with# `cargo bench --bench prune --features real-repo-benches`.real-repo-benches = []diff --git a/benches/CLAUDE.md b/benches/CLAUDE.mdindex ca0b4ad12..a4608f41a 100644--- a/benches/CLAUDE.md+++ b/benches/CLAUDE.md@@ -34,7 +34,7 @@ cargo bench --bench picker_preview warm # warm only## Rust Repo Caching-Real repo benchmarks clone rust-lang/rust on first run (~2-5 minutes). The clone is cached in `target/bench-repos/` and reused. Corrupted caches are auto-recovered.+Real repo benchmarks clone rust-lang/rust on first run (~2-5 minutes). The clone is cached in `~/.cache/wt-perf/bench-repos/` and reused. Corrupted caches are auto-recovered.## Faster Iteration@@ -183,7 +183,7 @@ diverged branches as backdrop, 200 commits, 100 files; `prune_real_repo` runswarm and probe-cold dry-runs on the `prune-real` fixture β a rust-lang/rust clone with 12squash-merged candidate pairs + 24 diverged worktrees and 24 divergedbranches, i.e. 36 linked worktrees, cached and self-repairing in-`target/bench-repos/rust-prune-12-24/`. That group is opt-in β+`~/.cache/wt-perf/bench-repos/rust-prune-12-24/`. That group is opt-in β`cargo bench --bench prune --features real-repo-benches prune_real_repo` βbecause its ~15 GiB fixture must never build on a hosted CI runner):@@ -251,7 +251,7 @@ cargo run -p wt-perf -- timeline -- -C /tmp/prune-repo step prune --min-age 0s**Live prune at real scale is a one-shot timeline, not a criterion group** βeach live run consumes the candidates, and re-creating them on rust costsminutes per iteration. The `prune-real[-M-U]` fixture is built for this-workflow: it lives in `target/bench-repos/` (no `--path`), and after a live+workflow: it lives in `~/.cache/wt-perf/bench-repos/` (no `--path`), and after a liveprune the next `wt-perf setup prune-real` or `prune_real_repo` bench rundetects the consumed candidates and re-creates just them (~1 min) instead ofrebuilding the ~15 GiB fixture. Don't run two builders concurrently (a bench@@ -262,7 +262,7 @@ invalidated for measures a degraded scan:```bashcargo run -p wt-perf -- setup prune-real # build or validate/repair the cache-cargo run -p wt-perf -- timeline -- -C target/bench-repos/rust-prune-12-24/repo step prune --min-age 0s+cargo run -p wt-perf -- timeline -- -C ~/.cache/wt-perf/bench-repos/rust-prune-12-24/repo step prune --min-age 0scargo run -p wt-perf -- setup prune-real # repair the consumed candidates```@@ -281,9 +281,15 @@ count.## Output Locations+Fixture repos live under the wt-perf cache dir β `~/.cache/wt-perf/` (the XDG+cache dir on Linux and macOS alike), or `$WT_PERF_CACHE_DIR` if set. This is+per-user and shared across worktrees/checkouts, so an expensive fixture is built+once per machine and survives `cargo clean` (a `target/`-rooted cache is+per-worktree and clean-wiped). `wt-perf setup` prints the exact path.+- Results: `target/criterion/`-- Cached rust repo: `target/bench-repos/rust/`-- Cached rust prune fixture: `target/bench-repos/rust-prune-<M>-<U>/` (repo ++- Cached rust repo: `~/.cache/wt-perf/bench-repos/rust/`+- Cached rust prune fixture: `~/.cache/wt-perf/bench-repos/rust-prune-<M>-<U>/` (repo +sibling worktrees + `round` counter for candidate re-creation)- HTML reports: `target/criterion/*/report/index.html`@@ -294,7 +300,7 @@ Use `wt-perf` to set up benchmark repos and generate Chrome Trace Format for vis### Setting up benchmark repos```bash-# Set up a repo with 8 worktrees (persists at /tmp/wt-perf-typical-8; the+# Set up a repo with 8 worktrees (persists at ~/.cache/wt-perf/typical-8; the# next `setup typical-8` run wipes and rebuilds it)cargo run -p wt-perf -- setup typical-8@@ -309,12 +315,12 @@ cargo run -p wt-perf -- setup typical-8# benches/prune.rs)# prune-real[-M-U] - rust-lang/rust clone + M squash-merged candidate pairs# + U diverged worktrees/branches (default 12-24), cached-# and self-repairing in target/bench-repos/ (no --path);+# and self-repairing in ~/.cache/wt-perf/bench-repos/ (no --path);# first run clones from the network# picker-test - Config for wt switch interactive picker testing# Invalidate caches for cold run-cargo run -p wt-perf -- invalidate /tmp/wt-perf-typical-8+cargo run -p wt-perf -- invalidate ~/.cache/wt-perf/typical-8```### Generating traces@@ -329,7 +335,7 @@ Perfetto/chrome://tracing. `--cold` invalidates caches first.cargo run -p wt-perf -- timeline -- list --progressive# Cold-cache run (invalidates the traced repo β the `-C` arg, else cwd)-cargo run -p wt-perf -- timeline --cold -- -C /tmp/wt-perf-typical-8 list --progressive+cargo run -p wt-perf -- timeline --cold -- -C ~/.cache/wt-perf/typical-8 list --progressive# Chrome Trace Format JSON for Perfettocargo run -p wt-perf -- timeline --chrome -- list --progressive > trace.json@@ -389,8 +395,8 @@ Three questions drive `wt list` performance work:```bash# Trace on rust-lang/rust (must run benchmark first to clone)-cargo run --release -q -- -vv -C target/bench-repos/rust list --progressive --branches-cargo run -p wt-perf -- trace target/bench-repos/rust/.git/wt/logs/trace.jsonl > rust-trace.json+cargo run --release -q -- -vv -C ~/.cache/wt-perf/bench-repos/rust list --progressive --branches+cargo run -p wt-perf -- trace ~/.cache/wt-perf/bench-repos/rust/.git/wt/logs/trace.jsonl > rust-trace.json```## Key Performance Insightsdiff --git a/benches/prune.rs b/benches/prune.rsindex c91c4eb94..cbab05bc8 100644--- a/benches/prune.rs+++ b/benches/prune.rs@@ -175,7 +175,7 @@ fn bench_prune_e2e(c: &mut Criterion) {/// --write-tree` ~130 ms, `git status` over ~60k files β vs the synthetic/// fixture where probes bottom out at subprocess spawn. First run clones/// rust-lang/rust from the network (minutes), then builds ~36 worktrees at-/// ~3 s each; both are cached in `target/bench-repos/` across runs.+/// ~3 s each; both are cached in the wt-perf cache dir across runs.////// Dry-runs only, in two flavors: warm (steady-state re-scan) and probe-cold/// (`.git/wt/cache/` cleared per iteration β the "first prune after fetchingdiff --git a/src/commands/CLAUDE.md b/src/commands/CLAUDE.mdindex bca1fd18e..5adf53000 100644--- a/src/commands/CLAUDE.md+++ b/src/commands/CLAUDE.md@@ -10,7 +10,7 @@ When debugging TUI commands like `wt switch` (interactive picker), use the `tmuxcargo run -p wt-perf -- setup picker-test```-This creates a reproducible test repo at `/tmp/wt-perf-picker-test/`.+This creates a reproducible test repo at `~/.cache/wt-perf/picker-test/` (XDG cache dir on Linux and macOS; `WT_PERF_CACHE_DIR` overrides the root). `wt-perf setup` also prints the exact path.### 2. Test Interactively@@ -21,7 +21,7 @@ Load the `tmux-cli` skill, then use the `tmux-cli` tool. Install if needed: `uv```bash# Launch shell in test repopane=$(tmux-cli launch "zsh")-tmux-cli send "cd /tmp/wt-perf-picker-test" --pane=$pane+tmux-cli send "cd ~/.cache/wt-perf/picker-test" --pane=$panetmux-cli wait_idle --pane=$pane# Run with debug logging@@ -43,7 +43,7 @@ MCP terminals use pseudo-TTY, not real terminals. If tests pass in MCP but users```typescript// Create terminal and navigate to test repomcp__node-terminal__terminal_create({ sessionId: "test" })-mcp__node-terminal__terminal_write({ sessionId: "test", input: "cd /tmp/wt-perf-picker-test" })+mcp__node-terminal__terminal_write({ sessionId: "test", input: "cd ~/.cache/wt-perf/picker-test" })mcp__node-terminal__terminal_send_key({ sessionId: "test", key: "enter" })// Run with debug loggingdiff --git a/tests/helpers/wt-perf/Cargo.toml b/tests/helpers/wt-perf/Cargo.tomlindex 39176bf0a..351e64966 100644--- a/tests/helpers/wt-perf/Cargo.toml+++ b/tests/helpers/wt-perf/Cargo.toml@@ -15,6 +15,9 @@ description = "Performance testing and tracing tools for worktrunk"# For repo setuptempfile = "3.27"dunce = "1"+# Resolve the cache directory for reusable fixtures (XDG on macOS too,+# matching worktrunk's own base-dir strategy in src/config/user/path.rs)+etcetera = "0.11"# For CLIclap = { version = "4.6", features = ["derive"] }diff --git a/tests/helpers/wt-perf/src/lib.rs b/tests/helpers/wt-perf/src/lib.rsindex 215810fe1..e4a206277 100644--- a/tests/helpers/wt-perf/src/lib.rs+++ b/tests/helpers/wt-perf/src/lib.rs@@ -24,6 +24,7 @@//!//! See `wt-perf --help` for CLI usage.+use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};use std::path::{Path, PathBuf};use std::process::Command;use std::sync::OnceLock;@@ -114,7 +115,7 @@ impl RepoConfig {/// identical); the composite fixtures build varied states instead:/// `mixed-W-B` via `create_mixed_repo_at`, `prune-M-U` via/// [`create_prune_repo_at`]. (`prune-real[-M-U]` is not a `SetupConfig`:-/// it is cache-managed under `target/bench-repos/` and takes no path β see+/// it is cache-managed under `<cache>/bench-repos/` and takes no path β see/// [`ensure_prune_real_repo`].)pub enum SetupConfig {Flat(RepoConfig),@@ -620,21 +621,39 @@ fn resolve_git_common_dir(repo_path: &Path) -> Option<PathBuf> {pointed.parent()?.parent().map(Path::to_path_buf)}-/// The shared cache for real-repo fixtures: `<workspace>/target/bench-repos`,-/// anchored to the workspace root at compile time so every entry point β-/// benches (cwd = workspace root) and the `wt-perf` CLI (cwd = anywhere) β-/// resolves the same cache.+/// Root of wt-perf's on-disk cache: `$WT_PERF_CACHE_DIR` if set, else the+/// per-user cache directory `<cache>/wt-perf` (`~/.cache/wt-perf`, or+/// `$XDG_CACHE_HOME/wt-perf`). `etcetera::choose_base_strategy` follows the+/// XDG convention on macOS too, matching worktrunk's own base-dir strategy+/// (`src/config/user/path.rs`), so the path is identical on Linux and macOS.+///+/// Both entry points β benches (cwd = workspace root) and the `wt-perf` CLI+/// (cwd = anywhere) β resolve the same machine-global path, so an expensive+/// fixture is built once per machine rather than once per git worktree. A+/// `target/`-rooted cache would be per-worktree (worktrees don't share+/// `target/`) and wiped by `cargo clean`; the cache dir is neither.+pub fn wt_perf_cache_dir() -> PathBuf {+ // Treat an empty value as unset: `WT_PERF_CACHE_DIR=` would otherwise yield+ // an empty root, so `setup <config>` would remove_dir_all a *relative*+ // `<config>` in the cwd instead of a dir under the cache.+ if let Some(dir) = std::env::var_os("WT_PERF_CACHE_DIR").filter(|d| !d.is_empty()) {+ return PathBuf::from(dir);+ }+ choose_base_strategy()+ .expect("no home directory; set WT_PERF_CACHE_DIR")+ .cache_dir()+ .join("wt-perf")+}++/// The shared cache for real, cloned upstream fixtures (rust-lang/rust and the+/// prune fixtures derived from it): `<cache>/bench-repos`.fn bench_repos_dir() -> PathBuf {- Path::new(env!("CARGO_MANIFEST_DIR"))- .ancestors()- .nth(3)- .unwrap()- .join("target/bench-repos")+ wt_perf_cache_dir().join("bench-repos")}/// Get or clone the rust-lang/rust repository for real-world benchmarks.///-/// The repo is cached at `target/bench-repos/rust` and reused across runs.+/// The repo is cached at `<cache>/bench-repos/rust` and reused across runs.fn ensure_rust_repo() -> PathBuf {RUST_REPO.get_or_init(|| {@@ -1182,7 +1201,7 @@ pub const PRUNE_REAL_UNMERGED: usize = 24;/// Each linked worktree materializes a full working tree: ~400 MiB and ~3 s/// per worktree, so the default populations build in minutes and take ~15 GiB./// Prefer [`ensure_prune_real_repo`], which builds once into-/// `target/bench-repos` and repairs consumed candidates on later runs.+/// `<cache>/bench-repos` and repairs consumed candidates on later runs.fn create_prune_real_repo_at(merged: usize, unmerged: usize, base_path: &Path) {clone_rust_repo_at(base_path);add_diverged_backdrop(base_path, unmerged, unmerged);@@ -1257,7 +1276,7 @@ fn next_squash_round(repo: &Path) -> usize {/// Get or build the cached rust-scale prune fixture, returning the repo path.///-/// The fixture lives at `target/bench-repos/rust-prune-<merged>-<unmerged>/repo`+/// The fixture lives at `<cache>/bench-repos/rust-prune-<merged>-<unmerged>/repo`/// (worktrees as siblings) so its minutes-long build (`create_prune_real_repo_at`)/// is paid once, not per bench run. On reuse it is validated by/// `prune_fixture_state`:diff --git a/tests/helpers/wt-perf/src/main.rs b/tests/helpers/wt-perf/src/main.rsindex 9767991b7..6e5c0cbba 100644--- a/tests/helpers/wt-perf/src/main.rs+++ b/tests/helpers/wt-perf/src/main.rs@@ -10,7 +10,7 @@ use std::time::{Duration, Instant};use clap::{Parser, Subcommand};use wt_perf::{PRUNE_REAL_MERGED, PRUNE_REAL_UNMERGED, canonicalize, ensure_prune_real_repo,- invalidate_caches_auto, parse_config, parse_pair,+ invalidate_caches_auto, parse_config, parse_pair, wt_perf_cache_dir,};#[derive(Parser)]@@ -28,7 +28,7 @@ enum Commands {/// Config name: typical-N, branches-N, branches-N-M, divergent, mixed-W-B, prune-M-U, prune-real[-M-U], picker-testconfig: String,- /// Directory to create repo in (default: temp directory)+ /// Directory to create repo in (default: wt-perf cache directory)#[arg(long)]path: Option<PathBuf>,},@@ -75,8 +75,8 @@ enum Commands {# Cold-cache run (invalidates the traced repo, then runs)wt-perf timeline --cold -- list- # Cold run against a specific repo- wt-perf timeline --cold -- -C /tmp/wt-perf-typical-1 list+ # Cold run against a specific repo (setup prints the exact path)+ wt-perf timeline --cold -- -C ~/.cache/wt-perf/typical-1 list# Chrome Trace Format JSON for Perfettowt-perf timeline --chrome -- list > trace.json@@ -102,7 +102,7 @@ fn main() {match cli.command {Commands::Setup { config, path } => {// `prune-real[-M-U]`: cache-managed rust-scale fixture (built once- // under target/bench-repos/, repaired after a live prune consumes+ // under the wt-perf cache dir, repaired after a live prune consumes// its candidates) β takes no --path and never offers cleanup.// Tested before parse_config so its `prune-` arm never sees it.let prune_real = if config == "prune-real" {@@ -113,7 +113,8 @@ fn main() {if let Some((merged, unmerged)) = prune_real {if path.is_some() {eprintln!(- "prune-real fixtures are cache-managed under target/bench-repos/; --path is not supported"+ "prune-real fixtures are cache-managed under {}; --path is not supported",+ wt_perf_cache_dir().join("bench-repos").display());std::process::exit(1);}@@ -155,7 +156,7 @@ fn main() {" prune-M-U - M squash-merged candidates + U unmerged worktrees/branches (wt step prune workload)");eprintln!(- " prune-real[-M-U] - rust-lang/rust clone + M squash-merged candidates + U unmerged worktrees/branches, cached in target/bench-repos (default {PRUNE_REAL_MERGED}-{PRUNE_REAL_UNMERGED}; first run clones from network)"+ " prune-real[-M-U] - rust-lang/rust clone + M squash-merged candidates + U unmerged worktrees/branches, cached in the wt-perf cache dir (default {PRUNE_REAL_MERGED}-{PRUNE_REAL_UNMERGED}; first run clones from network)");eprintln!(" picker-test - Config for wt switch interactive picker testing");std::process::exit(1);@@ -165,12 +166,12 @@ fn main() {std::fs::create_dir_all(&p).unwrap();canonicalize(&p).unwrap()} else {- let temp = std::env::temp_dir().join(format!("wt-perf-{}", config));- if temp.exists() {- std::fs::remove_dir_all(&temp).unwrap();+ let dir = wt_perf_cache_dir().join(&config);+ if dir.exists() {+ std::fs::remove_dir_all(&dir).unwrap();}- std::fs::create_dir_all(&temp).unwrap();- canonicalize(&temp).unwrap()+ std::fs::create_dir_all(&dir).unwrap();+ canonicalize(&dir).unwrap()};eprintln!("Creating {} repo...", config);