diff --git a/agents/conductors/hygiene/hygiene.sh b/agents/conductors/hygiene/hygiene.sh index f08205d3..73e2e7f3 100755 --- a/agents/conductors/hygiene/hygiene.sh +++ b/agents/conductors/hygiene/hygiene.sh @@ -552,7 +552,15 @@ fi # status vs the default branch) and stashes across the managed checkouts. Echoes # TSV rows: "\t\t\t". enumerate_condemn_candidates() { - local repo dir def br cur + local repo dir def br cur held claimed + # Branches claimed by live tasks in PyAutoMind/active.md ("\t"), + # read once through the worktree guard's own reader. A task branch lives in + # its own worktree, so it is never the canonical checkout's HEAD; with no + # commits yet it reads merged=yes, and tidy used to recommend deleting the + # branches of live tasks (PyAutoBrain#586). No registry → no claims, and the + # worktree check below still protects every checked-out branch. + claimed="$( (. "$HERE/../../../bin/worktree.sh" && worktree_list_claimed) 2>/dev/null \ + | awk -F'\t' '{print $2 "\t" $3}')" for repo in "${CODE_REPOS[@]}"; do dir="$(repo_dir "$repo")" repo_is_checked_out "$repo" || continue @@ -563,10 +571,16 @@ enumerate_condemn_candidates() { # commit, so without this it always reads merged=yes and is recommended for # straight delete: every agent session would be told to bin its own branch. cur=$(git -C "$dir" symbolic-ref --quiet --short HEAD 2>/dev/null) + # Every branch checked out in ANY worktree of this repo (task worktrees + # included) is being worked on, not debris. + held="$(git -C "$dir" worktree list --porcelain 2>/dev/null \ + | sed -n 's@^branch refs/heads/@@p')" while IFS= read -r br; do [[ -z "$br" ]] && continue case "$br" in main|master|HEAD|"$def") continue ;; esac [[ -n "$cur" && "$br" == "$cur" ]] && continue + grep -qxF -- "$br" <<<"$held" && continue + grep -qxF -- "$repo"$'\t'"$br" <<<"$claimed" && continue local merged=no if git -C "$dir" merge-base --is-ancestor "$br" "origin/$def" 2>/dev/null \ || git -C "$dir" merge-base --is-ancestor "$br" "$def" 2>/dev/null; then diff --git a/bin/clean_slate.sh b/bin/clean_slate.sh index e9a6a902..ffee6143 100755 --- a/bin/clean_slate.sh +++ b/bin/clean_slate.sh @@ -152,8 +152,21 @@ dataset_candidates() { return 0 } -for dir in */; do - repo="${dir%/}" +# Enumerate checkouts through the shared resolver, never `*/` at the root: the +# workspace groups repos (`fit/PyAutoFit`, `organs/PyAutoNerves`, ...), and a +# root glob saw none of them, so every scope silently found nothing +# (PyAutoBrain#586). `$repo` stays the root-relative path the body acts on; +# `$repo_name` is what the name-keyed lists are matched against. +. "$SCRIPT_DIR/_repo_paths.sh" +mapfile -t REPO_DIRS < <( + pyauto_repo_list "$ROOT" | while IFS=$'\t' read -r _ path; do + [ -n "$path" ] && printf '%s\n' "${path#"$ROOT"/}" + done | sort -u +) +# An empty list is the old silent no-op this replaced: say so and stop. +[ "${#REPO_DIRS[@]}" -gt 0 ] || { echo "clean_slate: no checkouts resolved under $ROOT" >&2; exit 1; } +for repo in "${REPO_DIRS[@]}"; do + repo_name="${repo##*/}" [ -d "$repo/.git" ] || continue header="" @@ -171,7 +184,7 @@ for dir in */; do # 1b. Classify untracked datasets by write site, remove the regenerable # ones, report orphans, and flag oversized committed ones. - if is_dataset_repo "$repo"; then + if is_dataset_repo "$repo_name"; then mapfile -t candidates < <(dataset_candidates "$repo") if [ "${#candidates[@]}" -gt 0 ]; then errfile=$(mktemp) @@ -269,7 +282,7 @@ for dir in */; do # roots. Keep assistant/workspace build products outside this narrow scope. # Never match nested domain directories named build, and never clean a # candidate containing tracked files even though git clean would retain them. - if is_packaging_repo "$repo"; then + if is_packaging_repo "$repo_name"; then while IFS= read -r -d '' d; do rel="${d#"$repo"/}" git -C "$repo" check-ignore -q -- "$rel" 2>/dev/null || continue diff --git a/tests/test_clean_slate.py b/tests/test_clean_slate.py index 0e85c197..c8c9c34e 100644 --- a/tests/test_clean_slate.py +++ b/tests/test_clean_slate.py @@ -11,6 +11,7 @@ # clean_slate.sh only sweeps datasets in the repos it knows are workspaces. DATASET_REPO = "autolens_workspace" +PACKAGING_REPO = "PyAutoGalaxy" def _init_repo(root, name, ignore="*.egg-info/\nbuild/\n"): @@ -338,6 +339,8 @@ def test_provenance_helper_failure_aborts_the_sweep(tmp_path): broken.mkdir() (broken / "dataset_provenance.py").write_text("import sys\nsys.exit(3)\n") (broken / "clean_slate.sh").write_text(CLEAN_SLATE.read_text()) + # The copy resolves checkouts through the real shared resolver. + (broken / "_repo_paths.sh").symlink_to(CLEAN_SLATE.parent / "_repo_paths.sh") env = {**os.environ, "PYAUTO_ROOT": str(tmp_path)} result = subprocess.run( @@ -362,3 +365,26 @@ def test_unparseable_script_warns_without_crashing_the_sweep(tmp_path): assert result.returncode == 0, result.stderr assert "WARN unparseable" in result.stderr assert not dataset.exists() + + +def test_packaging_reaches_repos_grouped_under_a_family_directory(tmp_path): + """The workspace groups repos (`fit/PyAutoFit`); a root `*/` glob saw none + of them, so every scope silently found nothing (PyAutoBrain#586).""" + family = tmp_path / "grp" + family.mkdir() + repo = _init_repo(family, PACKAGING_REPO) + _write(repo / f"{PACKAGING_REPO.lower()}.egg-info") + + result = _run(tmp_path, dry_run=True) + + assert result.returncode == 0, result.stderr + assert f"=== grp/{PACKAGING_REPO} ===" in result.stdout + assert ".egg-info/" in result.stdout + assert (repo / f"{PACKAGING_REPO.lower()}.egg-info").exists() # dry run + + +def test_no_resolved_checkout_is_an_error_not_a_silent_no_op(tmp_path): + result = _run(tmp_path, dry_run=True) + + assert result.returncode == 1 + assert "no checkouts resolved" in result.stderr diff --git a/tests/test_hygiene_conductor.py b/tests/test_hygiene_conductor.py index 671ab4dc..e9eda55f 100644 --- a/tests/test_hygiene_conductor.py +++ b/tests/test_hygiene_conductor.py @@ -1046,6 +1046,47 @@ def test_tidy_still_condemns_a_branch_that_is_not_checked_out(tmp_path): assert [c["locator"] for c in doc["candidates"]] == ["feature/spent"] + +def test_tidy_never_condemns_a_branch_held_by_another_worktree(tmp_path): + # Regression for #586: a task branch lives in its OWN worktree, so it is + # never the canonical HEAD; with no commits yet it reads merged=yes and + # tidy recommended a straight delete of live task branches. + repo = tmp_path / "PyAutoBrain" + _init_git_repo_with_branch(repo, "claude/some-agent-run-abc123") + git = ["git", "-C", str(repo)] + subprocess.run([*git, "branch", "feature/spent"], check=True, capture_output=True) + subprocess.run( + [*git, "worktree", "add", "-q", "-b", "feature/live-task", str(tmp_path / "wt")], + check=True, + capture_output=True, + ) + + doc = json.loads(_run(["tidy", "--json"], tmp_path).stdout) + + assert [c["locator"] for c in doc["candidates"]] == ["feature/spent"] + + +def test_tidy_never_condemns_a_branch_claimed_in_the_registry(tmp_path): + # Claimed in PyAutoMind/active.md but with no worktree on disk yet (or one + # on another machine): still live work, never debris (#586). + repo = tmp_path / "PyAutoBrain" + _init_git_repo_with_branch(repo, "claude/some-agent-run-abc123") + git = ["git", "-C", str(repo)] + for branch in ("feature/spent", "feature/claimed-task"): + subprocess.run([*git, "branch", branch], check=True, capture_output=True) + mind = tmp_path / "PyAutoMind" + mind.mkdir() + (mind / "active.md").write_text( + "## claimed-task\n" + "- status: library-dev\n" + "- repos:\n" + " - PyAutoBrain: feature/claimed-task\n" + ) + + doc = json.loads(_run(["tidy", "--json"], tmp_path).stdout) + + assert [c["locator"] for c in doc["candidates"]] == ["feature/spent"] + # --- Body-map-derived coverage ------------------------------------------------- # # The conductor scans repositories, so WHICH repositories must come from the body