Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: clean up stale trash in `wt config state clear` (#1960)
Maximilian Roos850961366d9c
diff --git a/docs/content/faq.md b/docs/content/faq.mdindex ece3088c3..2819f42c7 100644--- a/docs/content/faq.md+++ b/docs/content/faq.md@@ -117,10 +117,11 @@ Worktrunk stores small amounts of cache and log data in the repository's `.git/`| `.git/wt/cache/ci-status/*.json` | CI status cache (~1KB each) | `wt list` when `gh` or `glab` CLI is installed || `.git/wt/logs/*.log` | Background command output | Hooks, background `wt remove` || `.git/wt/logs/commands.jsonl` | Command audit log (~2MB max) | Hooks, LLM commands |+| `.git/wt/trash/<name>-<timestamp>` | Staged worktree contents pending background deletion | `wt remove` |None of this is tracked by git or pushed to remotes.-**To remove:** `wt config state clear` removes all worktrunk keys from `.git/config`, deletes CI cache, and clears logs.+**To remove:** `wt config state clear` removes all worktrunk keys from `.git/config`, deletes CI cache, clears logs, and removes stale trash.### What Worktrunk does NOT create@@ -153,7 +154,7 @@ Use `-D` to force-delete branches with unmerged changes. Use `--no-delete-branch### Other cleanup-- `wt config state clear` — removes cached state from `.git/config` and clears CI cache/logs+- `wt config state clear` — removes cached state from `.git/config`, clears CI cache/logs, and removes stale trash from worktree removal- `wt config shell uninstall` — removes shell integration from rc filesSee [What files does Worktrunk create?](#what-files-does-worktrunk-create) for details.diff --git a/skills/worktrunk/reference/faq.md b/skills/worktrunk/reference/faq.mdindex b5a77e45d..24cfdf816 100644--- a/skills/worktrunk/reference/faq.md+++ b/skills/worktrunk/reference/faq.md@@ -110,10 +110,11 @@ Worktrunk stores small amounts of cache and log data in the repository's `.git/`| `.git/wt/cache/ci-status/*.json` | CI status cache (~1KB each) | `wt list` when `gh` or `glab` CLI is installed || `.git/wt/logs/*.log` | Background command output | Hooks, background `wt remove` || `.git/wt/logs/commands.jsonl` | Command audit log (~2MB max) | Hooks, LLM commands |+| `.git/wt/trash/<name>-<timestamp>` | Staged worktree contents pending background deletion | `wt remove` |None of this is tracked by git or pushed to remotes.-**To remove:** `wt config state clear` removes all worktrunk keys from `.git/config`, deletes CI cache, and clears logs.+**To remove:** `wt config state clear` removes all worktrunk keys from `.git/config`, deletes CI cache, clears logs, and removes stale trash.### What Worktrunk does NOT create@@ -146,7 +147,7 @@ Use `-D` to force-delete branches with unmerged changes. Use `--no-delete-branch### Other cleanup-- `wt config state clear` — removes cached state from `.git/config` and clears CI cache/logs+- `wt config state clear` — removes cached state from `.git/config`, clears CI cache/logs, and removes stale trash from worktree removal- `wt config shell uninstall` — removes shell integration from rc filesSee [What files does Worktrunk create?](#what-files-does-worktrunk-create) for details.diff --git a/src/cli/config.rs b/src/cli/config.rsindex 639844fb5..113227d17 100644--- a/src/cli/config.rs+++ b/src/cli/config.rs@@ -766,6 +766,7 @@ CI cache entries show status, age, and the commit SHA they were fetched for."#)]- All CI status cache- All hints- All log files+- Stale trash from worktree removal (`.git/wt/trash/`)Use individual subcommands (`default-branch clear`, `ci-status clear --all`, etc.)to clear specific state."#)]diff --git a/src/commands/config/state.rs b/src/commands/config/state.rsindex 48e3c388f..b9936ff06 100644--- a/src/commands/config/state.rs+++ b/src/commands/config/state.rs@@ -47,6 +47,38 @@ fn is_wt_log_file(path: &std::path::Path) -> bool {name.ends_with(".log") || name.ends_with(".jsonl") || name.ends_with(".jsonl.old")}+/// Clear stale entries from the wt/trash directory.+///+/// Worktree removal renames directories into `.git/wt/trash/` for instant UX,+/// then deletes them in a background process. If the background `rm -rf` fails+/// or is killed, entries accumulate. This cleans them up.+fn clear_trash(repo: &Repository) -> anyhow::Result<usize> {+ let trash_dir = repo.wt_trash_dir();++ if !trash_dir.exists() {+ return Ok(0);+ }++ let mut cleared = 0;+ for entry in std::fs::read_dir(&trash_dir)? {+ let entry = entry?;+ let path = entry.path();+ if path.is_dir() {+ std::fs::remove_dir_all(&path)?;+ } else {+ std::fs::remove_file(&path)?;+ }+ cleared += 1;+ }++ // Remove the trash directory itself if empty+ if std::fs::read_dir(&trash_dir)?.next().is_none() {+ let _ = std::fs::remove_dir(&trash_dir);+ }++ Ok(cleared)+}+/// Clear all log files from the wt/logs directoryfn clear_logs(repo: &Repository) -> anyhow::Result<usize> {let log_dir = repo.wt_logs_dir();@@ -609,6 +641,12 @@ pub fn handle_state_clear_all() -> anyhow::Result<()> {cleared_any = true;}+ // Clear stale trash from worktree removal+ let trash_cleared = clear_trash(&repo)?;+ if trash_cleared > 0 {+ cleared_any = true;+ }+if cleared_any {eprintln!("{}", success_message("Cleared all stored state"));} else {diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rsindex a2d1b46fb..622acbdc2 100644--- a/tests/integration_tests/config_state.rs+++ b/tests/integration_tests/config_state.rs@@ -675,6 +675,30 @@ fn test_state_clear_all_comprehensive(repo: TestRepo) {assert!(!log_dir.exists());}+#[rstest]+fn test_state_clear_all_cleans_trash(repo: TestRepo) {+ // Create trash directory with stale entries (simulating failed background rm)+ let git_dir = repo.root_path().join(".git");+ let trash_dir = git_dir.join("wt/trash");+ std::fs::create_dir_all(trash_dir.join("myproject.feature-1234567890/target")).unwrap();+ std::fs::write(+ trash_dir.join("myproject.feature-1234567890/target/.rustc_info.json"),+ "{}",+ )+ .unwrap();+ std::fs::create_dir_all(trash_dir.join("myproject.bugfix-9999999999")).unwrap();+ // Stray file directly in trash (not inside a subdirectory) — exercises the+ // non-directory branch in clear_trash's `if path.is_dir()` guard.+ std::fs::write(trash_dir.join("stray-file.txt"), "stale").unwrap();++ let output = wt_state_clear_all_cmd(&repo).output().unwrap();+ assert!(output.status.success());+ assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"[32m✓[39m [32mCleared all stored state[39m");++ // Trash directory itself should be removed (empty after cleanup)+ assert!(!trash_dir.exists(), "Trash directory should be cleaned up");+}+#[rstest]fn test_state_clear_all_nothing_to_clear(repo: TestRepo) {// First clear to ensure nothing existsdiff --git a/tests/snapshots/integration__integration_tests__help__help_config_state_clear.snap b/tests/snapshots/integration__integration_tests__help__help_config_state_clear.snapindex 0d86ebdb4..749ab02e8 100644--- a/tests/snapshots/integration__integration_tests__help__help_config_state_clear.snap+++ b/tests/snapshots/integration__integration_tests__help__help_config_state_clear.snap@@ -61,6 +61,7 @@ Clears all stored state:- All CI status cache- All hints- All log files+- Stale trash from worktree removal ([2m.git/wt/trash/[0m)Use individual subcommands ([2mdefault-branch clear[0m, [2mci-status clear --all[0m, etc.)to clear specific state.