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:
@@ -53,6 +53,12 @@ impl WorktreeManager {
|
||||
/// The override root gets the repo's basename too: without it two repos
|
||||
/// spawning the same branch name target one directory and the second
|
||||
/// fails with `PathExists`.
|
||||
/// Where [`Self::add_new`] / [`Self::add_existing`] would place this
|
||||
/// branch's worktree. Callers use it to check ownership before creating.
|
||||
pub fn planned_path(&self, base_repo: &Path, branch: &str) -> PathBuf {
|
||||
self.target_path(base_repo, branch)
|
||||
}
|
||||
|
||||
fn target_path(&self, base_repo: &Path, branch: &str) -> PathBuf {
|
||||
// An unusable branch name falls back to a digest rather than escaping
|
||||
// the root; `run_add` surfaces git's own error for it anyway.
|
||||
@@ -97,6 +103,16 @@ impl WorktreeManager {
|
||||
// path, so a previously-leaked entry doesn't block this branch.
|
||||
prune(base_repo)?;
|
||||
if path.symlink_metadata().is_ok() {
|
||||
// A tree we left behind for this very branch — the dirty-work
|
||||
// guard refuses to delete unsaved work, and the path is
|
||||
// deterministic, so erroring here would burn the branch
|
||||
// permanently: no spawn could ever produce a worktree for it
|
||||
// again and the product offers no way to clear it. Adopt it
|
||||
// instead; landing in the operator's own unsaved work on the
|
||||
// branch they asked for is what they wanted (#67 review).
|
||||
if registered_for(base_repo, &path, branch) {
|
||||
return Ok(path);
|
||||
}
|
||||
return Err(WorktreeError::PathExists { path });
|
||||
}
|
||||
// The parent (`<repo>/worktrees` or the override root) may not exist
|
||||
@@ -176,6 +192,34 @@ fn main_checkout(path: &Path) -> PathBuf {
|
||||
.unwrap_or_else(|| path.to_path_buf())
|
||||
}
|
||||
|
||||
/// True when `path` is already a registered worktree of `base_repo` holding
|
||||
/// `branch` — i.e. one this daemon created for this branch and later declined
|
||||
/// to remove.
|
||||
fn registered_for(base_repo: &Path, path: &Path, branch: &str) -> bool {
|
||||
let Ok(out) = Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(base_repo)
|
||||
.args(["worktree", "list", "--porcelain"])
|
||||
.output()
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let want = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
|
||||
let text = String::from_utf8_lossy(&out.stdout);
|
||||
let mut here = false;
|
||||
for line in text.lines() {
|
||||
if let Some(p) = line.strip_prefix("worktree ") {
|
||||
let p = PathBuf::from(p);
|
||||
here = p.canonicalize().unwrap_or(p) == want;
|
||||
} else if here {
|
||||
if let Some(r) = line.strip_prefix("branch ") {
|
||||
return r.trim() == format!("refs/heads/{branch}");
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// `Some(inner)` when a registered worktree lives under `path`. Git omits
|
||||
/// those directories from `git status`, so the dirty-work guard cannot see
|
||||
/// them and removal would delete them (#54 review).
|
||||
@@ -202,12 +246,20 @@ fn nested_worktree(path: &Path) -> Option<PathBuf> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Short, stable digest used to disambiguate override roots.
|
||||
/// Short digest used to disambiguate override roots.
|
||||
///
|
||||
/// Hand-rolled FNV-1a rather than `DefaultHasher`: this value becomes a
|
||||
/// directory name, and `DefaultHasher`'s algorithm is explicitly not
|
||||
/// guaranteed stable across Rust releases — a toolchain bump would remap every
|
||||
/// repo to a new directory and orphan the worktrees under the old one
|
||||
/// (#67 review).
|
||||
fn short_hash(s: &str) -> String {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
s.hash(&mut h);
|
||||
format!("{:08x}", h.finish() as u32)
|
||||
let mut h: u32 = 0x811c_9dc5;
|
||||
for b in s.as_bytes() {
|
||||
h ^= u32::from(*b);
|
||||
h = h.wrapping_mul(0x0100_0193);
|
||||
}
|
||||
format!("{h:08x}")
|
||||
}
|
||||
|
||||
/// The relative directory a branch's worktree lives in.
|
||||
|
||||
Reference in New Issue
Block a user