fix(maestro): fifth-review findings, incl. the guarded-worktree dead end

The cleanup guard plus deterministic paths had created a trap: one
untracked file an agent left behind — a log, a .env, a stray artifact —
made the guard refuse removal, the directory stayed, and every later
spawn of that branch hit PathExists. The branch was burned permanently
and the product offered no way to clear it. A leftover tree registered
for the same branch is now adopted, so the operator lands back in their
own unsaved work. A tree a *live* session holds is still rejected, since
two sessions must not share a checkout.

Also:

- Fast-forward still awaited /api/info, /api/projects and /api/names
  inline, the exact freeze the draft loaders were restructured to avoid.
  It posts to the same channel now.
- Draft updates were routed by tab index, which shifts when a tab closes:
  an in-flight load could be dropped or land on the wrong pane. Drafts
  carry an id and are found by it.
- extend_names left the name it had just displayed at the head of the
  queue, so the next reroll produced it again and the key looked dead.
- The fast-forward fallback discarded info.home_dir, so its draft showed
  unabbreviated paths and seeded "Other directory…" with / — disagreeing
  with the normal draft entry point.
- Web fast-forward mounted via openSession, which replaces the focused
  pane: pressing it while attached evicted that session from the layout.
  It reuses an empty pane or splits, as the TUI does.
- branchEdited was inferred from the seeded branch value, but in "new"
  mode that value is the auto-synced session name — so any form rebuild
  froze the branch against later rerolls.
- resolveBaseRepo ignored the worktree checkbox, rebuilding the fieldset
  expanded under an unticked box and probing gitroot for a worktree the
  operator had turned off.
- short_hash used DefaultHasher, whose algorithm is not stable across
  Rust releases, in a directory name — a toolchain bump would orphan
  every worktree under the override root. Now FNV-1a.
This commit is contained in:
Miguel Palhas
2026-08-24 00:28:50 +01:00
parent e9368bb242
commit bbbc24a4aa
9 changed files with 310 additions and 54 deletions
+43
View File
@@ -399,3 +399,46 @@ fn similar_branch_names_get_distinct_directories() {
assert_ne!(a, c);
assert!(a.ends_with("feat/x"), "{a:?}");
}
/// The dead end the cleanup guard created: it refuses to delete unsaved work,
/// the path is deterministic, and `PathExists` is a hard error — so one stray
/// untracked file used to burn that branch permanently, with no way to clear
/// it from the product. The leftover is adopted instead (#67 review).
#[test]
fn a_guarded_leftover_is_adopted_rather_than_burning_the_branch() {
let repo_dir = TempDir::new().unwrap();
let repo = repo_dir.path().to_path_buf();
init_repo(&repo);
let mgr = WorktreeManager::new(None);
let first = mgr.add_new(&repo, "feat/resume").expect("add");
std::fs::write(first.join("notes.txt"), b"unsaved\n").unwrap();
// The guard refuses, leaving it on disk.
mgr.remove(&repo, &first)
.expect_err("dirty tree is guarded");
assert!(first.exists());
// Spawning that branch again lands in the operator's own work, not an error.
let again = mgr
.add_existing(&repo, "feat/resume")
.expect("leftover is adopted");
assert_eq!(again, first);
assert!(
again.join("notes.txt").exists(),
"their work is still there"
);
}
/// Adoption must not extend to a directory that merely sits in the way.
#[test]
fn an_unrelated_directory_at_the_target_path_is_still_an_error() {
let repo_dir = TempDir::new().unwrap();
let repo = repo_dir.path().to_path_buf();
init_repo(&repo);
std::fs::create_dir_all(repo.join("worktrees").join("feat").join("squatter")).unwrap();
let mgr = WorktreeManager::new(None);
mgr.add_new(&repo, "feat/squatter")
.expect_err("a non-worktree directory must not be adopted");
}