Skip to main content

orch/engine/
cleanup.rs

1//! Worktree cleanup and merged-PR detection.
2//!
3//! Contains the post-merge cleanup pipeline: removing git worktrees,
4//! deleting local/remote branches, pulling main, and detecting
5//! already-merged PRs so their tasks can be marked done.
6
7use crate::backends::{ExternalBackend, ExternalId, ExternalTask, Status};
8use crate::cmd::CommandErrorContext;
9use crate::engine::ci_bootstrap;
10use crate::engine::tasks::{TaskManager, store_task_to_external};
11use crate::store;
12use crate::store::TaskStatus;
13use crate::store::TaskStore;
14use crate::store::store_log_activity;
15use dashmap::{DashMap, DashSet};
16use futures::future;
17use std::collections::HashMap;
18use std::collections::HashSet;
19use std::env;
20use std::fs as std_fs;
21use std::io::ErrorKind;
22use std::path::Path;
23use std::path::PathBuf;
24use std::process::Output;
25use std::sync::Arc;
26use std::sync::LazyLock;
27use std::time::SystemTime;
28use std::time::{Duration, Instant};
29use tokio::fs;
30use tokio::process::Command;
31use tokio::time::sleep;
32use tokio::time::timeout;
33
34use super::sync::ReviewTaskSnapshot;
35use crate::config;
36use crate::engine;
37use crate::engine::auto_merge;
38use crate::engine::runner::cow;
39use crate::engine::runner::git_ops;
40use crate::engine::runner::screenshots::SCREENSHOTS_BRANCH;
41use crate::engine::runner::worktree;
42use crate::github::http::GhHttp;
43use crate::home;
44
45/// Reconciliation calls can be slow on repos with many issues or during GitHub instability.
46/// Cleanup runs in a background task (tokio::spawn) so it does not block the main tick loop.
47/// 30 s gives enough headroom for paginated list_all_tasks calls while still bounding the
48/// overall background-task duration.
49const RECONCILIATION_LIST_TIMEOUT: Duration = Duration::from_secs(30);
50/// Closed-issue reconciliation is a best-effort cleanup fallback, not a per-sync requirement.
51/// Run it on a slower cadence so repeated GitHub slowness does not consume 30 s every sync.
52const RECONCILIATION_INTERVAL: Duration = Duration::from_mins(15);
53
54const RUN_OUTPUT_RETENTION_DAYS: i32 = 30;
55const RUN_PRUNE_INTERVAL_SECS: i64 = 24 * 60 * 60;
56const RUN_PRUNE_KV_KEY: &str = "task_runs_pruned_at";
57
58/// Per-repo in-flight guard for closed-issue reconciliation/cleanup.
59/// Prevents overlapping cleanup runs from piling up across sync cycles.
60static CLEANUP_IN_FLIGHT: LazyLock<DashSet<String>> = LazyLock::new(DashSet::new);
61/// Per-repo reconciliation cadence tracker.
62static RECONCILIATION_LAST_RUN: LazyLock<DashMap<String, Instant>> = LazyLock::new(DashMap::new);
63
64struct CleanupInFlightGuard {
65    repo: String,
66}
67
68impl CleanupInFlightGuard {
69    fn try_acquire(repo: &str) -> Option<Self> {
70        if CLEANUP_IN_FLIGHT.insert(repo.to_string()) {
71            Some(Self {
72                repo: repo.to_string(),
73            })
74        } else {
75            None
76        }
77    }
78}
79
80impl Drop for CleanupInFlightGuard {
81    fn drop(&mut self) {
82        CLEANUP_IN_FLIGHT.remove(&self.repo);
83    }
84}
85
86fn should_run_reconciliation(repo: &str) -> bool {
87    let now = Instant::now();
88    if let Some(mut last_run) = RECONCILIATION_LAST_RUN.get_mut(repo) {
89        if last_run.elapsed() < RECONCILIATION_INTERVAL {
90            return false;
91        }
92        *last_run = now;
93        true
94    } else {
95        RECONCILIATION_LAST_RUN.insert(repo.to_string(), now);
96        true
97    }
98}
99
100/// Options controlling the worktree janitor.
101#[derive(Debug, Clone)]
102pub struct JanitorOptions {
103    /// Minimum age (in hours) of the worktree directory before it is eligible for removal.
104    ///
105    /// A worktree whose filesystem mtime is more recent than this threshold is skipped
106    /// even if the task is already in a terminal state. This provides a safety buffer
107    /// against races (e.g. review agent still writing when the janitor runs).
108    ///
109    /// Default: 24.  Configurable via `workflow.worktree_janitor_ttl_hours` in config.
110    pub ttl_hours: u64,
111
112    /// When `true`, log what would be done but skip all destructive operations.
113    ///
114    /// Configurable via `workflow.worktree_janitor_dry_run: true` in config.
115    pub dry_run: bool,
116}
117
118impl Default for JanitorOptions {
119    fn default() -> Self {
120        Self {
121            ttl_hours: 24,
122            dry_run: false,
123        }
124    }
125}
126
127impl JanitorOptions {
128    /// Read options from the global config, falling back to defaults.
129    pub fn from_config() -> Self {
130        let ttl_hours = config::get("workflow.worktree_janitor_ttl_hours")
131            .ok()
132            .and_then(|v| v.parse::<u64>().ok())
133            .unwrap_or(24);
134
135        let dry_run = config::get("workflow.worktree_janitor_dry_run").is_ok_and(|v| v == "true");
136
137        Self { ttl_hours, dry_run }
138    }
139}
140
141/// Cleanup worktrees for completed tasks.
142///
143/// Targets tasks that are finished: status:done OR closed on GitHub
144/// (e.g. when a PR merge auto-closes the issue via "Fixes #N").
145///
146/// Note: This is a fallback. Primary cleanup happens inline in
147/// `auto_merge_pr` via `cleanup_task_worktree`. This catches edge cases
148/// where the inline cleanup missed (e.g., manual merges, auto-closed issues).
149pub(crate) async fn cleanup_done_worktrees(
150    backend: &Arc<dyn ExternalBackend>,
151    repo: &str,
152    task_manager: &Arc<TaskManager>,
153    store: &Arc<TaskStore>,
154) -> anyhow::Result<()> {
155    let opts = JanitorOptions::from_config();
156    cleanup_done_worktrees_with_opts(backend, repo, task_manager, store, &opts).await
157}
158
159/// Clear the stored output of runs for tasks done longer than `RUN_OUTPUT_RETENTION_DAYS`, once a day
160pub(crate) async fn prune_old_run_output(store: &TaskStore) -> anyhow::Result<()> {
161    let now = chrono::Utc::now().timestamp();
162    let last = store
163        .kv_get(RUN_PRUNE_KV_KEY)
164        .await?
165        .and_then(|v| v.parse::<i64>().ok())
166        .unwrap_or(0);
167    if now - last < RUN_PRUNE_INTERVAL_SECS {
168        return Ok(());
169    }
170    store.kv_set(RUN_PRUNE_KV_KEY, &now.to_string()).await?;
171
172    let pruned = store.prune_old_runs(RUN_OUTPUT_RETENTION_DAYS).await?;
173    if pruned > 0 {
174        tracing::info!(pruned, "cleared the output of old task runs");
175    }
176    Ok(())
177}
178
179async fn reconcile_closed_tasks(
180    task_ids: &mut Vec<String>,
181    all_tasks: &[ExternalTask],
182    task_manager: &TaskManager,
183    store: &Arc<TaskStore>,
184    repo: &str,
185) {
186    const MAX_RECONCILE_PER_CYCLE: usize = 10;
187
188    let done_set: HashSet<String> = task_ids.iter().cloned().collect();
189    let mut closed_not_in_store = Vec::new();
190    for task in all_tasks {
191        if task.state != "closed" || done_set.contains(&task.id.0) {
192            continue;
193        }
194        if ci_bootstrap::hold_after_merge(task, store, repo).await {
195            continue;
196        }
197        closed_not_in_store.push(task);
198    }
199
200    // Split: issues that already have status:done label just need worktree
201    // cleanup (no API call needed), vs those needing label reconciliation.
202    let mut already_labeled: Vec<String> = Vec::new();
203    let mut needs_reconcile: Vec<&ExternalTask> = Vec::new();
204    for task in &closed_not_in_store {
205        if task.labels.iter().any(|l| l == "status:done") {
206            already_labeled.push(task.id.0.clone());
207        } else {
208            needs_reconcile.push(task);
209        }
210    }
211
212    // Add already-labeled issues directly (no API call).
213    if !already_labeled.is_empty() {
214        tracing::debug!(
215            count = already_labeled.len(),
216            "closed issues already labeled status:done — adding to cleanup set"
217        );
218        task_ids.extend(already_labeled);
219    }
220
221    // Reconcile issues missing the done label — cap to avoid rate-limit
222    // exhaustion. Remaining issues will be picked up in subsequent cycles.
223    if needs_reconcile.len() > MAX_RECONCILE_PER_CYCLE {
224        tracing::info!(
225            total = needs_reconcile.len(),
226            batch = MAX_RECONCILE_PER_CYCLE,
227            "capping closed-issue reconciliation to avoid rate-limit exhaustion"
228        );
229    }
230    let mut futs = Vec::new();
231    for task in needs_reconcile.iter().take(MAX_RECONCILE_PER_CYCLE) {
232        let id = task.id.clone();
233        let task_id_str = task.id.0.clone();
234        let labels = task.labels.clone();
235        futs.push(async move {
236            tracing::info!(
237                task_id = task_id_str,
238                labels = ?labels,
239                "closed issue with stale status label — reconciling to done and cleaning up"
240            );
241            match task_manager.update_task_status(&id, Status::Done).await {
242                Ok(()) => Some(task_id_str),
243                Err(e) => {
244                    tracing::warn!(
245                        task_id = task_id_str,
246                        err = %e,
247                        "failed to reconcile closed issue status to done"
248                    );
249                    None
250                }
251            }
252        });
253    }
254    let results = future::join_all(futs).await;
255    task_ids.extend(results.into_iter().flatten());
256}
257
258/// Cleanup worktrees for completed tasks with explicit options.
259///
260/// Separated from `cleanup_done_worktrees` so that integration tests can
261/// inject specific options (e.g. `ttl_hours: 0`, `dry_run: true`) without
262/// touching the global config.
263pub(crate) async fn cleanup_done_worktrees_with_opts(
264    backend: &Arc<dyn ExternalBackend>,
265    repo: &str,
266    task_manager: &Arc<TaskManager>,
267    store: &Arc<TaskStore>,
268    opts: &JanitorOptions,
269) -> anyhow::Result<()> {
270    let Some(_guard) = CleanupInFlightGuard::try_acquire(repo) else {
271        tracing::debug!(
272            repo = %repo,
273            "cleanup already in progress for repo — skipping overlapping run"
274        );
275        return Ok(());
276    };
277
278    // Read done tasks from the store first; fall back to backend before first sync.
279    let done_tasks = task_manager.list_all_by_status(Status::Done).await?;
280    tracing::debug!(count = done_tasks.len(), "checking done tasks for cleanup");
281
282    // Collect task IDs from external done tasks.
283    let mut task_ids: Vec<String> = done_tasks.iter().map(|t| t.id.0.clone()).collect();
284
285    // Also include closed-but-not-done external tasks.
286    // When a PR merge auto-closes a GitHub issue (via "Fixes #N"), the issue
287    // state becomes "closed" but orch never updates the status label to "done".
288    // These orphaned worktrees accumulate unless we catch them here.
289    // Note: This still queries the backend because GitHub `state` (open/closed)
290    // is backend-specific and not tracked in the store.
291    //
292    // Use list_reconciliation_candidates() (closed issues only, bounded recent window) directly.
293    // list_all_tasks() fetches every issue ever created (state=all, all pages) and
294    // consistently times out on repos with many issues — it is not appropriate for
295    // a cleanup fallback path.
296    if should_run_reconciliation(repo) {
297        match timeout(
298            RECONCILIATION_LIST_TIMEOUT,
299            backend.list_reconciliation_candidates(),
300        )
301        .await
302        {
303            Ok(Ok(candidates)) if !candidates.is_empty() => {
304                tracing::debug!(
305                    count = candidates.len(),
306                    "reconciling closed issues from candidates"
307                );
308                reconcile_closed_tasks(&mut task_ids, &candidates, task_manager, store, repo).await;
309            }
310            Ok(Ok(_)) => {
311                tracing::debug!("no reconciliation candidates found");
312            }
313            Ok(Err(e)) => {
314                tracing::warn!(err = %e, "failed to list reconciliation candidates");
315            }
316            Err(_) => {
317                tracing::warn!(
318                    timeout_secs = RECONCILIATION_LIST_TIMEOUT.as_secs(),
319                    retry_after_secs = RECONCILIATION_INTERVAL.as_secs(),
320                    "timed out listing reconciliation candidates"
321                );
322            }
323        }
324    } else {
325        tracing::debug!(
326            retry_after_secs = RECONCILIATION_INTERVAL.as_secs(),
327            "skipping closed-issue reconciliation until interval elapses"
328        );
329    }
330
331    // Also include internal done tasks.
332    if let Ok(internal_done) = task_manager.list_internal_by_status(TaskStatus::Done).await {
333        task_ids.extend(internal_done.iter().map(|t| t.id.0.clone()));
334    }
335
336    tracing::debug!(
337        count = task_ids.len(),
338        "checking all terminal tasks for cleanup"
339    );
340
341    if let Err(e) = auto_merge::sweep_ci_timer_keys(store, repo, &task_ids).await {
342        tracing::warn!(err = %e, "failed to sweep CI timer keys");
343    }
344
345    // Prefetch all cleaned external IDs in one query instead of N+1 per-task lookups.
346    // NOTE: This is now primarily a hint to skip processing, NOT a correctness
347    // guarantee — tasks with external_id NOT in the store at all (or mismatched
348    // repo) will still be cleaned by iterating all worktree directories.
349    let already_cleaned = store.cleaned_external_ids(repo).await.unwrap_or_default();
350
351    let mut cleaned_any = false;
352    for task_id in &task_ids {
353        // Skip if already cleaned (checked via prefetched set).
354        if already_cleaned.contains(task_id) {
355            continue;
356        }
357
358        match cleanup_task_worktree_with_opts(task_id, repo, store, opts).await {
359            Ok(true) => cleaned_any = true,
360            Ok(false) => {
361                // Skipped (TTL guard, tmux guard, or nothing to remove) — do not
362                // pull main; we did not change the repo state.
363            }
364            Err(e) => {
365                tracing::warn!(task_id, err = %e, "worktree cleanup failed for task");
366            }
367        }
368    }
369
370    // ── Orphaned worktree scanner ─────────────────────────────────────────────
371    //
372    // Even if every task in our list was already cleaned (or had no worktree),
373    // there may be worktree directories on disk that are not owned by any task.
374    // This happens when a task was ingested into the store but its worktree_cleaned
375    // flag was never set — e.g. because the task was never in `list_all_by_status`
376    // (unknown repo / missing from store at cleanup time), or the cleanup skipped
377    // due to TTL guard and never retried.
378    //
379    // We scan all worktree directories in this repo's worktrees subdirectory and
380    // remove any that are not referenced by an existing task. This catches the
381    // "done but worktree not cleaned" case that `orch service doctor` reports.
382    let worktrees_base = match home::worktrees_dir() {
383        Ok(base) => Some(base),
384        Err(e) => {
385            tracing::debug!(err = %e, "worktrees_dir() unavailable — skipping orphan scan");
386            None
387        }
388    };
389
390    if let Some(base) = worktrees_base {
391        let project = repo
392            .rsplit('/')
393            .next()
394            .unwrap_or(repo)
395            .trim_end_matches(".git");
396        let repo_worktrees = base.join(project);
397
398        // Build set of all known worktree paths for this repo (all statuses).
399        let mut combined: HashSet<String> = HashSet::new();
400        if let Ok(tasks) = store.list_all(repo).await {
401            for t in tasks {
402                if !t.worktree.is_empty() {
403                    combined.insert(t.worktree);
404                }
405            }
406        }
407
408        // Also include active worktrees (in case task is in-memory but has a dir).
409        if let Ok(project_dir) = resolve_project_dir_for_repo(repo)
410            && let Ok(active_wts) = worktree::list_project_worktrees(&project_dir).await
411        {
412            for wt in active_wts {
413                combined.insert(wt.to_string_lossy().to_string());
414            }
415        }
416
417        // Scan all branch directories and remove orphaned ones.
418        if let Ok(mut entries) = fs::read_dir(&repo_worktrees).await {
419            while let Ok(Some(entry)) = entries.next_entry().await {
420                let Ok(entry_meta) = entry.metadata().await else {
421                    continue;
422                };
423                if !entry_meta.is_dir() {
424                    continue;
425                }
426                let wt_path = entry.path().to_string_lossy().to_string();
427                if combined.contains(&wt_path) {
428                    continue;
429                }
430                tracing::info!(worktree = %wt_path, "found orphaned worktree — attempting cleanup");
431                match resolve_repo_root_for_orphaned_worktree(&entry.path()).await {
432                    Ok(repo_root) => {
433                        let removed = remove_worktree_and_branch(
434                            "orphan-scan",
435                            &entry.path(),
436                            None,
437                            Path::new(&repo_root),
438                            false,
439                        )
440                        .await;
441                        if removed {
442                            tracing::info!(worktree = %wt_path, "orphaned worktree removed");
443                        } else {
444                            tracing::warn!(worktree = %wt_path, "orphaned worktree could not be removed");
445                        }
446                    }
447                    Err(e) => {
448                        tracing::warn!(worktree = %wt_path, err = %e, "could not resolve repo root for orphaned worktree");
449                    }
450                }
451            }
452        }
453    }
454
455    // Prune stale worktree metadata and pull main after cleanup.
456    if cleaned_any
457        && !opts.dry_run
458        && let Ok(repo_root) = resolve_repo_root(repo).await
459    {
460        const GIT_TIMEOUT: Duration = Duration::from_secs(60);
461
462        // Prune stale .git/worktrees/ entries whose directories no longer exist.
463        let prune_result = timeout(
464            GIT_TIMEOUT,
465            Command::new("git")
466                .args(["-C", &repo_root, "worktree", "prune"])
467                .output_with_context(),
468        )
469        .await;
470        match prune_result {
471            Ok(Ok(output)) if output.status.success() => {
472                tracing::debug!(%repo, "pruned stale worktree metadata");
473            }
474            Ok(Ok(output)) => {
475                let stderr = String::from_utf8_lossy(&output.stderr);
476                tracing::warn!(%repo, err = %stderr, "git worktree prune failed");
477            }
478            Ok(Err(e)) => {
479                tracing::warn!(%repo, err = %e, "git worktree prune failed");
480            }
481            Err(_) => {
482                tracing::warn!(%repo, "git worktree prune timed out after 60s");
483            }
484        }
485
486        // Fetch instead of pull: refresh `origin/*` refs without touching
487        // the operator's checked-out branch or working tree. `pull` would
488        // operate on whatever HEAD happens to be (often a feature branch,
489        // not main) and would silently no-op on divergence.
490        let fetch_result = timeout(
491            GIT_TIMEOUT,
492            git_ops::git()
493                .await
494                .args(["-C", &repo_root, "fetch", "--prune", "origin"])
495                .output_with_context(),
496        )
497        .await;
498        match fetch_result {
499            Ok(Ok(output)) if output.status.success() => {
500                tracing::info!(%repo, "fetched origin after worktree cleanup");
501            }
502            Ok(Ok(output)) => {
503                let stderr = String::from_utf8_lossy(&output.stderr);
504                tracing::debug!(%repo, err = %stderr, "git fetch skipped after cleanup");
505            }
506            Ok(Err(e)) => {
507                tracing::debug!(%repo, err = %e, "git fetch failed after cleanup");
508            }
509            Err(_) => {
510                tracing::debug!(%repo, "git fetch timed out after 60s");
511            }
512        }
513    }
514
515    Ok(())
516}
517
518/// Cleanup a single task's worktree and branches.
519///
520/// Removes the git worktree, deletes local + remote branches,
521/// fetches origin to refresh remote tracking refs, and marks the task as cleaned.
522///
523/// This function is used for inline post-merge cleanup (called from
524/// auto-merge flows) and must attempt immediate removal — it constructs
525/// janitor options with `ttl_hours = 0` so the janitor age guard does not
526/// postpone removing freshly-created worktrees.
527pub(crate) async fn cleanup_task_worktree(
528    task_id: &str,
529    repo: &str,
530    store: &Arc<TaskStore>,
531) -> anyhow::Result<()> {
532    let opts = JanitorOptions {
533        ttl_hours: 0,
534        ..Default::default()
535    };
536    let cleaned = cleanup_task_worktree_with_opts(task_id, repo, store, &opts).await?;
537
538    // Fetch origin once after inline cleanup (post-merge single-task path).
539    // Only fetch if cleanup actually happened (not skipped due to active session).
540    // Fetch (not pull) so the operator's checked-out branch and working tree
541    // are never touched — see the matching comment in cleanup_orphaned_worktrees.
542    if cleaned && let Ok(repo_root) = resolve_repo_root(repo).await {
543        const GIT_TIMEOUT: Duration = Duration::from_secs(60);
544        let fetch_result = timeout(
545            GIT_TIMEOUT,
546            git_ops::git()
547                .await
548                .args(["-C", &repo_root, "fetch", "--prune", "origin"])
549                .output_with_context(),
550        )
551        .await;
552        match fetch_result {
553            Ok(Ok(output)) if output.status.success() => {
554                tracing::debug!(%repo, "fetched origin after task cleanup");
555            }
556            Ok(Ok(output)) => {
557                let stderr = String::from_utf8_lossy(&output.stderr);
558                tracing::debug!(%repo, err = %stderr, "git fetch skipped after task cleanup");
559            }
560            Ok(Err(e)) => {
561                tracing::debug!(%repo, err = %e, "git fetch failed after task cleanup");
562            }
563            Err(_) => {
564                tracing::debug!(%repo, "git fetch timed out after 60s");
565            }
566        }
567    }
568
569    Ok(())
570}
571
572/// Cleanup a single task's worktree and branches with explicit janitor options.
573///
574/// Returns `Ok(true)` when cleanup was actually performed and the task was
575/// marked as cleaned.  Returns `Ok(false)` when cleanup was intentionally
576/// skipped (TTL guard, active tmux session guard, or nothing on disk to remove).
577/// Returns `Err` on unexpected failures (e.g. cannot resolve the repo root).
578pub(crate) async fn cleanup_task_worktree_with_opts(
579    task_id: &str,
580    repo: &str,
581    store: &Arc<TaskStore>,
582    opts: &JanitorOptions,
583) -> anyhow::Result<bool> {
584    let (worktree, branch, keep_remote_branch, merged, external_id, pr_merged) =
585        store::opt_store_get_task(&Some(Arc::clone(store)), repo, task_id)
586            .await
587            .map_or((None, None, false, false, None, false), |t| {
588                // Blocked tasks with a PR should keep their remote branch so the
589                // PR stays open for human review.
590                let keep = t.status == TaskStatus::Blocked && t.pr_number.is_some();
591                let merged = t.status == TaskStatus::Done;
592                (
593                    Some(t.worktree),
594                    Some(t.branch),
595                    keep,
596                    merged,
597                    t.external_id,
598                    // Done means the PR merged, and a squash leaves the branch commits on no ref
599                    merged && t.pr_number.is_some(),
600                )
601            });
602
603    let worktree_path = worktree.as_ref().map(PathBuf::from);
604
605    // Get worktrees base path
606    let worktrees_base = home::worktrees_dir().unwrap_or_else(|_| PathBuf::from(".orch/worktrees"));
607
608    // Determine which directory to remove.
609    //
610    // Priority:
611    //   1. stored "worktree" path, if it exists on disk.
612    //   2. Construct from worktrees_base + project name + branch, if it exists.
613    let worktree_to_remove = if let Some(ref wt) = worktree_path {
614        if match fs::try_exists(wt).await {
615            Ok(true) => true,
616            Ok(false) => false,
617            Err(e) => {
618                tracing::warn!(task_id, worktree = %wt.display(), err = %e, "try_exists failed on stored worktree path — trying branch-based fallback");
619                false
620            }
621        } {
622            Some(wt.clone())
623        } else {
624            // The recorded worktree path doesn't exist — try reconstructing from branch.
625            if let Some(ref b) = branch {
626                let project = repo
627                    .rsplit('/')
628                    .next()
629                    .unwrap_or(repo)
630                    .trim_end_matches(".git");
631                let candidate = worktrees_base.join(project).join(b);
632                if match fs::try_exists(&candidate).await {
633                    Ok(true) => true,
634                    Ok(false) => false,
635                    Err(e) => {
636                        tracing::warn!(task_id, err = %e, "try_exists failed — assuming candidate does not exist");
637                        false
638                    }
639                } {
640                    Some(candidate)
641                } else {
642                    None
643                }
644            } else {
645                None
646            }
647        }
648    } else if let Some(ref b) = branch {
649        // No worktree path in store — try branch-based path.
650        let project = repo
651            .rsplit('/')
652            .next()
653            .unwrap_or(repo)
654            .trim_end_matches(".git");
655        let candidate = worktrees_base.join(project).join(b);
656        if match fs::try_exists(&candidate).await {
657            Ok(true) => true,
658            Ok(false) => false,
659            Err(e) => {
660                tracing::warn!(task_id, err = %e, "try_exists failed — assuming candidate does not exist");
661                false
662            }
663        } {
664            Some(candidate)
665        } else {
666            None
667        }
668    } else {
669        None
670    };
671
672    let mut did_clean = false;
673
674    // If there's nothing to remove (no worktree path and no branch),
675    // treat as a no-op and return Ok(false) without attempting to
676    // resolve the repo root. Resolving the repo root can fail for
677    // projects not registered in config and that should not make
678    // cleanup a hard error when there is nothing to do.
679    let branch_nonempty = branch.as_ref().filter(|b| !b.is_empty());
680    if worktree_to_remove.is_none() && branch_nonempty.is_none() {
681        // If the stored worktree path is non-empty but doesn't exist on disk,
682        // the worktree is already gone — mark it cleaned so we stop retrying.
683        if worktree_path
684            .as_ref()
685            .is_some_and(|p| !p.as_os_str().is_empty())
686        {
687            tracing::debug!(
688                task_id,
689                worktree = ?worktree_path,
690                "stored worktree path no longer exists on disk — marking cleaned"
691            );
692            if let Ok(Some(store_id)) = store.resolve_task_id(repo, task_id).await
693                && let Err(e) = store.mark_cleaned(store_id).await
694            {
695                tracing::warn!(task_id, err = %e, "failed to mark worktree cleaned in store — will retry");
696            }
697        }
698        return Ok(false);
699    }
700
701    if let Some(wt) = worktree_to_remove {
702        // TTL guard: skip if the worktree directory is too young.
703        if let Some(age_hours) = worktree_age_hours(&wt).await
704            && age_hours < opts.ttl_hours
705        {
706            tracing::debug!(
707                task_id,
708                worktree = %wt.display(),
709                age_hours,
710                ttl_hours = opts.ttl_hours,
711                "worktree too young for cleanup, skipping"
712            );
713            return Ok(false);
714        }
715
716        // Tmux guard: skip if any active pane still has its cwd inside this worktree.
717        if is_worktree_in_active_session(&wt).await {
718            tracing::warn!(
719                task_id,
720                worktree = %wt.display(),
721                "worktree is referenced by an active tmux session — skipping cleanup"
722            );
723            return Ok(false);
724        }
725
726        // Validate git metadata before attempting any git operations.
727        // If the .git file points to a deleted gitdir the worktree is
728        // unrecoverable — force-remove the directory and skip git commands.
729        let gitdir_valid = worktree::validate_worktree_gitdir(&wt).await;
730
731        if opts.dry_run {
732            tracing::info!(
733                task_id,
734                worktree = %wt.display(),
735                branch = ?branch,
736                gitdir_valid,
737                "[dry-run] would remove worktree and branch"
738            );
739        } else if !gitdir_valid {
740            tracing::warn!(
741                task_id,
742                worktree = %wt.display(),
743                "worktree has broken git metadata — force-removing directory without git operations"
744            );
745            match fs::remove_dir_all(&wt).await {
746                Ok(()) => {
747                    did_clean = true;
748                }
749                Err(e) => {
750                    tracing::warn!(
751                        task_id,
752                        worktree = %wt.display(),
753                        err = %e,
754                        "failed to force-remove broken worktree directory"
755                    );
756                }
757            }
758        } else {
759            tracing::info!(task_id, worktree = %wt.display(), "removing worktree");
760            // Derive repo root from the worktree itself (for cross-project support).
761            // If the worktree belongs to a different project than the current repo
762            // context (e.g., internal task with worktree in another project), we can
763            // still find the repo root by examining the worktree's .git file.
764            let repo_root = match resolve_repo_root_from_worktree(&wt).await {
765                Ok(root) => root,
766                Err(_) => resolve_repo_root(repo).await?,
767            };
768            if let Some(br) = branch.as_deref().filter(|b| !b.is_empty())
769                && !pr_merged
770                && !rescue_and_notify(
771                    task_id,
772                    repo,
773                    external_id.as_deref(),
774                    Some(&wt),
775                    br,
776                    Path::new(&repo_root),
777                )
778                .await
779            {
780                return Ok(false);
781            }
782            if merged {
783                cow::warm_from_merged(
784                    Path::new(&repo_root),
785                    &worktree::project_name(Path::new(&repo_root)),
786                    &wt,
787                )
788                .await;
789            }
790            let removed = remove_worktree_and_branch_unchecked(
791                task_id,
792                &wt,
793                branch.as_deref(),
794                Path::new(&repo_root),
795                keep_remote_branch,
796            )
797            .await;
798            if removed {
799                did_clean = true;
800            } else {
801                tracing::warn!(
802                    task_id,
803                    worktree = %wt.display(),
804                    "worktree directory still exists after removal attempt — not marking as cleaned"
805                );
806            }
807        }
808    } else if let Some(ref br) = branch {
809        // Worktree directory is already gone, but the branch may still exist
810        // on the remote. Delete it to avoid orphaned branches.
811        if !opts.dry_run && !keep_remote_branch {
812            tracing::debug!(task_id, branch = %br, "no worktree on disk, cleaning up branch only");
813            // Resolve repo root lazily — branch cleanup needs it.
814            let repo_root = resolve_repo_root(repo).await?;
815            if !pr_merged
816                && !rescue_and_notify(
817                    task_id,
818                    repo,
819                    external_id.as_deref(),
820                    None,
821                    br,
822                    Path::new(&repo_root),
823                )
824                .await
825            {
826                return Ok(false);
827            }
828            delete_branches(task_id, br, Path::new(&repo_root)).await;
829            did_clean = true;
830        }
831    }
832
833    if did_clean {
834        // Mark as cleaned in store so we don't retry next tick.
835        // If resolve_task_id returns None the task is not (or no longer) in
836        // the store; that is fine — we already did the on-disk work.
837        if let Ok(Some(store_id)) = store.resolve_task_id(repo, task_id).await
838            && let Err(e) = store.mark_cleaned(store_id).await
839        {
840            tracing::warn!(task_id, err = %e, "failed to mark worktree cleaned — will retry");
841            // Don't log activity; retry next tick will do the actual cleanup
842            return Ok(did_clean);
843        }
844        store_log_activity(
845            &Some(Arc::clone(store)),
846            repo,
847            task_id,
848            "branch_delete",
849            None,
850            None,
851            None,
852            None,
853            Some(&serde_json::json!({
854                "worktree": worktree_path.map(|p| p.display().to_string()),
855                "branch": branch,
856                "keep_remote_branch": keep_remote_branch,
857            })),
858        )
859        .await;
860    }
861
862    Ok(did_clean)
863}
864
865/// Result of saving a task branch's unpushed work before cleanup deletes it
866#[derive(Debug, PartialEq, Eq)]
867pub(crate) enum Rescue {
868    /// Every commit is on a remote ref already
869    Clean,
870    /// The commits were pushed to `rescue/<branch>`
871    Pushed {
872        branch: String,
873        commits: Vec<String>,
874    },
875    /// Git failed, so the worktree and branch must stay
876    Failed,
877}
878
879async fn git_in(dir: &Path, args: &[&str]) -> Option<Output> {
880    git_ops::git()
881        .await
882        .arg("-C")
883        .arg(dir)
884        .args(args)
885        .output_with_context()
886        .await
887        .ok()
888}
889
890/**
891 * Cleanup deletes the worktree and the local branch, which can be the only refs to commits the
892 * agent made before its run ended without a push (#3999). Uncommitted changes are committed
893 * first, then everything not on a remote ref goes to `rescue/<branch>`
894 **/
895pub(crate) async fn rescue_unpushed_work(
896    task_id: &str,
897    wt: Option<&Path>,
898    br: &str,
899    repo_root: &Path,
900) -> Rescue {
901    if let Some(wt) = wt
902        && fs::try_exists(wt.join(".git")).await.unwrap_or(false)
903    {
904        let Some(status) = git_in(wt, &["status", "--porcelain"]).await else {
905            tracing::warn!(task_id, branch = %br, "cannot read worktree status, keeping worktree");
906            return Rescue::Failed;
907        };
908        if !status.status.success() {
909            tracing::warn!(task_id, branch = %br, "git status failed, keeping worktree");
910            return Rescue::Failed;
911        }
912        if !status.stdout.is_empty() {
913            let committed = git_in(wt, &["add", "-A"])
914                .await
915                .is_some_and(|o| o.status.success())
916                && git_in(
917                    wt,
918                    &[
919                        "-c",
920                        "user.name=orch",
921                        "-c",
922                        "user.email=orch@localhost",
923                        "commit",
924                        "-m",
925                        "rescue: uncommitted changes saved by orch cleanup",
926                    ],
927                )
928                .await
929                .is_some_and(|o| o.status.success());
930            if !committed {
931                tracing::warn!(task_id, branch = %br, "cannot commit uncommitted changes, keeping worktree");
932                return Rescue::Failed;
933            }
934        }
935    }
936
937    let local_ref = format!("refs/heads/{br}");
938    let exists = git_in(repo_root, &["rev-parse", "--verify", "--quiet", &local_ref])
939        .await
940        .is_some_and(|o| o.status.success());
941    if !exists {
942        return Rescue::Clean;
943    }
944    // Commits on the local default branch survive the branch delete, so they do not count
945    let default_ref = format!(
946        "refs/heads/{}",
947        worktree::detect_default_branch(repo_root).await
948    );
949    // A bare clone has no remote-tracking refs, so ask the remote for the branch tip
950    let remote_tip = git_in(repo_root, &["ls-remote", "origin", &local_ref])
951        .await
952        .filter(|o| o.status.success())
953        .and_then(|o| {
954            String::from_utf8_lossy(&o.stdout)
955                .split_whitespace()
956                .next()
957                .map(str::to_string)
958        });
959    // A tip someone else pushed may be missing locally, and rev-list fails on an unknown object
960    let remote_tip = match remote_tip {
961        Some(tip) => {
962            let known = git_in(repo_root, &["cat-file", "-e", &format!("{tip}^{{commit}}")])
963                .await
964                .is_some_and(|o| o.status.success());
965            known.then_some(tip)
966        }
967        None => None,
968    };
969    let mut args = vec!["rev-list", "--oneline", &local_ref, "--not", "--remotes"];
970    if let Some(tip) = remote_tip.as_deref() {
971        args.push(tip);
972    }
973    let default_exists = default_ref != local_ref
974        && git_in(
975            repo_root,
976            &["rev-parse", "--verify", "--quiet", &default_ref],
977        )
978        .await
979        .is_some_and(|o| o.status.success());
980    if default_exists {
981        args.push(&default_ref);
982    }
983    let Some(list) = git_in(repo_root, &args)
984        .await
985        .filter(|o| o.status.success())
986    else {
987        tracing::warn!(task_id, branch = %br, "cannot list unpushed commits, keeping worktree and branch");
988        return Rescue::Failed;
989    };
990    let commits: Vec<String> = String::from_utf8_lossy(&list.stdout)
991        .lines()
992        .map(str::to_string)
993        .collect();
994    if commits.is_empty() {
995        return Rescue::Clean;
996    }
997
998    let rescue = format!("rescue/{br}");
999    let refspec = format!("{local_ref}:refs/heads/{rescue}");
1000    let pushed = git_in(
1001        repo_root,
1002        &["-c", "lfs.locksverify=false", "push", "origin", &refspec],
1003    )
1004    .await;
1005    if pushed.is_some_and(|o| o.status.success()) {
1006        tracing::warn!(task_id, branch = %br, rescue = %rescue, commits = commits.len(), "pushed unpushed commits to rescue branch");
1007        Rescue::Pushed {
1008            branch: rescue,
1009            commits,
1010        }
1011    } else {
1012        tracing::warn!(task_id, branch = %br, rescue = %rescue, "rescue push failed, keeping worktree and branch");
1013        Rescue::Failed
1014    }
1015}
1016
1017fn rescue_comment(rescue_branch: &str, commits: &[String]) -> String {
1018    let list = commits
1019        .iter()
1020        .map(|c| format!("- `{c}`"))
1021        .collect::<Vec<_>>()
1022        .join("\n");
1023    format!(
1024        "Cleanup found commits on this task's branch that were never pushed. They are saved on `{rescue_branch}`:\n\n{list}"
1025    )
1026}
1027
1028/// Save unpushed work and tell the task about it. Returns false when cleanup must not go on
1029async fn rescue_and_notify(
1030    task_id: &str,
1031    repo: &str,
1032    external_id: Option<&str>,
1033    wt: Option<&Path>,
1034    br: &str,
1035    repo_root: &Path,
1036) -> bool {
1037    match rescue_unpushed_work(task_id, wt, br, repo_root).await {
1038        Rescue::Clean => true,
1039        Rescue::Failed => false,
1040        Rescue::Pushed { branch, commits } => {
1041            if let Some(number) = external_id.filter(|n| n.parse::<u64>().is_ok()) {
1042                match GhHttp::new() {
1043                    Ok(gh) => {
1044                        if let Err(e) = gh
1045                            .add_comment(repo, number, &rescue_comment(&branch, &commits))
1046                            .await
1047                        {
1048                            tracing::warn!(task_id, err = %e, "failed to comment on rescue branch");
1049                        }
1050                    }
1051                    Err(e) => {
1052                        tracing::warn!(task_id, err = %e, "cannot comment on rescue branch");
1053                    }
1054                }
1055            }
1056            true
1057        }
1058    }
1059}
1060
1061/// Remove a git worktree directory and its local + remote branches.
1062/// When `keep_remote_branch` is true, only the worktree and local branch are
1063/// removed — the remote branch stays so that any linked PR remains open
1064/// (used for blocked tasks awaiting human review).
1065/// Returns `true` if the worktree directory is gone after the call
1066/// (either we removed it or it was already absent).
1067pub(crate) async fn remove_worktree_and_branch(
1068    task_id: &str,
1069    wt: &Path,
1070    branch: Option<&str>,
1071    repo_root: &Path,
1072    keep_remote_branch: bool,
1073) -> bool {
1074    remove_worktree_and_branch_for(task_id, wt, branch, repo_root, keep_remote_branch, false).await
1075}
1076
1077/// Same as `remove_worktree_and_branch`, but a merged PR skips the rescue. A squash leaves the
1078/// branch commits on no ref, so the rescue would push a branch for work that already landed
1079pub(crate) async fn remove_worktree_and_branch_for(
1080    task_id: &str,
1081    wt: &Path,
1082    branch: Option<&str>,
1083    repo_root: &Path,
1084    keep_remote_branch: bool,
1085    pr_merged: bool,
1086) -> bool {
1087    if !pr_merged
1088        && let Some(br) = branch.filter(|b| !b.is_empty())
1089        && rescue_unpushed_work(task_id, Some(wt), br, repo_root).await == Rescue::Failed
1090    {
1091        return false;
1092    }
1093
1094    remove_worktree_and_branch_unchecked(task_id, wt, branch, repo_root, keep_remote_branch).await
1095}
1096
1097/// The caller has already rescued the branch or knows its PR merged
1098async fn remove_worktree_and_branch_unchecked(
1099    task_id: &str,
1100    wt: &Path,
1101    branch: Option<&str>,
1102    repo_root: &Path,
1103    keep_remote_branch: bool,
1104) -> bool {
1105    let wt_str = wt.to_string_lossy().to_string();
1106    let repo_root_str = repo_root.to_string_lossy();
1107
1108    // Remove worktree FIRST, then delete the branch.
1109    // Git refuses to remove a worktree if its branch is already deleted.
1110    let remove_result = Command::new("git")
1111        .args([
1112            "-C",
1113            repo_root_str.as_ref(),
1114            "worktree",
1115            "remove",
1116            &wt_str,
1117            "--force",
1118        ])
1119        .output_with_context()
1120        .await;
1121
1122    match remove_result {
1123        Ok(output) if output.status.success() => {
1124            const MAX_RETRIES: usize = 3;
1125            const RETRY_DELAY_MS: u64 = 2000;
1126            tracing::info!(task_id, "worktree removed (git metadata)");
1127            // `git worktree remove` only removes git metadata — the physical directory
1128            // with working files may remain. Clean it up with retry logic, since macOS
1129            // sometimes needs a moment to release file handles from a recently-exited
1130            // process. Retrying with a delay addresses "Directory not empty" errors.
1131            let mut removed = false;
1132            for attempt in 0..MAX_RETRIES {
1133                match fs::remove_dir_all(wt).await {
1134                    Ok(()) => {
1135                        removed = true;
1136                        break;
1137                    }
1138                    Err(e) if e.kind() == ErrorKind::NotFound => {
1139                        // Directory was already removed by git or another cleanup pass.
1140                        removed = true;
1141                        break;
1142                    }
1143                    Err(e) if e.kind() == ErrorKind::DirectoryNotEmpty => {
1144                        if attempt < MAX_RETRIES - 1 {
1145                            tracing::debug!(
1146                                task_id,
1147                                path = %wt.display(),
1148                                attempt = attempt + 1,
1149                                "worktree directory not empty — retrying after delay"
1150                            );
1151                            sleep(Duration::from_millis(RETRY_DELAY_MS)).await;
1152                        } else {
1153                            tracing::warn!(
1154                                task_id,
1155                                path = %wt.display(),
1156                                err = %e,
1157                                "failed to remove worktree directory after {} attempts (Directory not empty)",
1158                                MAX_RETRIES
1159                            );
1160                        }
1161                    }
1162                    Err(e) => {
1163                        tracing::warn!(task_id, path = %wt.display(), err = %e, "failed to remove worktree directory");
1164                        break;
1165                    }
1166                }
1167            }
1168            if removed {
1169                tracing::info!(task_id, path = %wt.display(), "worktree physical directory removed");
1170            } else {
1171                tracing::warn!(
1172                    task_id,
1173                    path = %wt.display(),
1174                    "worktree directory still present after git worktree remove"
1175                );
1176            }
1177        }
1178        Ok(output) => {
1179            let stderr = String::from_utf8_lossy(&output.stderr);
1180            // "is not a working tree" means git's metadata is stale — the path
1181            // exists on disk but is no longer registered as a worktree. Treat
1182            // this as an already-gone worktree: physically remove the directory
1183            // and prune stale git metadata. Downgrade to debug — this is an
1184            // expected idempotency case, not a real error.
1185            if stderr.contains("is not a working tree") {
1186                tracing::debug!(
1187                    task_id,
1188                    path = %wt.display(),
1189                    "path is not a registered worktree (stale metadata) — removing directory and pruning"
1190                );
1191                if let Err(e) = fs::remove_dir_all(wt).await {
1192                    // Only warn if the directory actually still exists after removal attempt.
1193                    if match fs::try_exists(wt).await {
1194                        Ok(true) => true,
1195                        Ok(false) => false,
1196                        Err(e) => {
1197                            tracing::warn!(task_id, err = %e, "try_exists failed — assuming worktree does not exist");
1198                            false
1199                        }
1200                    } {
1201                        tracing::warn!(
1202                            task_id,
1203                            path = %wt.display(),
1204                            err = %e,
1205                            "failed to remove stale worktree directory"
1206                        );
1207                    }
1208                } else {
1209                    tracing::debug!(task_id, path = %wt.display(), "stale worktree directory removed");
1210                }
1211                // Prune stale worktree entries from git metadata so the warning
1212                // stops recurring on future `git worktree list` / cleanup cycles.
1213                if let Err(e) = Command::new("git")
1214                    .args(["-C", repo_root_str.as_ref(), "worktree", "prune"])
1215                    .output_with_context()
1216                    .await
1217                {
1218                    tracing::warn!(task_id, err = %e, "git worktree prune failed — stale metadata may persist");
1219                }
1220            } else {
1221                tracing::warn!(task_id, err = %stderr, "failed to remove worktree");
1222            }
1223        }
1224        Err(e) => {
1225            tracing::warn!(task_id, err = %e, "failed to remove worktree");
1226        }
1227    }
1228
1229    // Prune stale .git/worktrees/ entries left behind after removal.
1230    if let Err(e) = Command::new("git")
1231        .args(["-C", repo_root_str.as_ref(), "worktree", "prune"])
1232        .output_with_context()
1233        .await
1234    {
1235        tracing::warn!(task_id, err = %e, "git worktree prune failed — stale metadata may persist");
1236    }
1237
1238    // Delete local and remote branch from the main repo root (worktree is already gone).
1239    // When keep_remote_branch is set, only delete the local branch — the remote stays
1240    // so any linked PR remains open for human review.
1241    if let Some(br) = branch {
1242        if keep_remote_branch {
1243            delete_local_branch(task_id, br, repo_root).await;
1244        } else {
1245            delete_branches(task_id, br, repo_root).await;
1246        }
1247    }
1248
1249    // Definitively check: is the worktree directory actually gone?
1250    match fs::try_exists(wt).await {
1251        Ok(false) => true, // confirmed gone
1252        Ok(true) => false, // still there
1253        Err(e) => {
1254            tracing::warn!(task_id, err = %e, "try_exists failed — assuming worktree still present");
1255            false // conservative: don't claim success
1256        }
1257    }
1258}
1259
1260/// Commits reachable from `rev` and from none of `others`. `None` when git cannot answer
1261async fn commits_outside(repo_root: &Path, rev: &str, others: &[&str]) -> Option<u64> {
1262    let mut args = vec![
1263        "rev-list".to_string(),
1264        "--count".to_string(),
1265        rev.to_string(),
1266    ];
1267    for other in others {
1268        let exists = Command::new("git")
1269            .args([
1270                "-C",
1271                &repo_root.to_string_lossy(),
1272                "rev-parse",
1273                "--verify",
1274                "--quiet",
1275                other,
1276            ])
1277            .output_with_context()
1278            .await
1279            .is_ok_and(|o| o.status.success());
1280        if exists {
1281            args.push("--not".to_string());
1282            args.push(other.to_string());
1283        }
1284    }
1285    let output = Command::new("git")
1286        .arg("-C")
1287        .arg(repo_root)
1288        .args(&args)
1289        .output_with_context()
1290        .await
1291        .ok()?;
1292    if !output.status.success() {
1293        return None;
1294    }
1295    String::from_utf8_lossy(&output.stdout).trim().parse().ok()
1296}
1297
1298/// Delete the local branch only when origin or the default branch holds every commit on it
1299async fn delete_local_branch(task_id: &str, br: &str, repo_root: &Path) {
1300    let default_branch = worktree::detect_default_branch(repo_root).await;
1301    let remote_ref = format!("refs/remotes/origin/{br}");
1302    let default_ref = format!("refs/remotes/origin/{default_branch}");
1303    let local_ref = format!("refs/heads/{br}");
1304    let local_default_ref = format!("refs/heads/{default_branch}");
1305    let rescue_ref = format!("refs/remotes/origin/rescue/{br}");
1306    match commits_outside(
1307        repo_root,
1308        &local_ref,
1309        &[&remote_ref, &default_ref, &local_default_ref, &rescue_ref],
1310    )
1311    .await
1312    {
1313        Some(0) => {}
1314        Some(n) => {
1315            tracing::warn!(task_id, branch = %br, unpushed = n, "local branch holds commits origin does not have, keeping it");
1316            return;
1317        }
1318        None => {
1319            tracing::debug!(task_id, branch = %br, "local branch missing or unreadable, nothing to delete");
1320            return;
1321        }
1322    }
1323    let output = Command::new("git")
1324        .args(["-C", &repo_root.to_string_lossy(), "branch", "-D", br])
1325        .output_with_context()
1326        .await;
1327    match output {
1328        Ok(o) if o.status.success() => {
1329            tracing::info!(task_id, branch = %br, "local branch deleted");
1330        }
1331        Ok(o) => {
1332            let stderr = String::from_utf8_lossy(&o.stderr);
1333            tracing::debug!(task_id, err = %stderr, "local branch delete skipped (may not exist)");
1334        }
1335        Err(e) => {
1336            tracing::warn!(task_id, err = %e, "failed to delete local branch");
1337        }
1338    }
1339}
1340
1341/**
1342 * Delete local and remote branches for a task, but never the only copy of a commit.
1343 * The remote branch goes only when the default branch already contains all of it, which
1344 * covers fast-forward and merge-commit merges. A squash-merged branch stays, and GitHub's
1345 * "automatically delete head branches" removes it. Every other caller (startup rebase
1346 * failure, closed-issue reconciliation, orphan sweeps) may hold unmerged work (#3781)
1347 **/
1348pub(crate) async fn delete_branches(task_id: &str, br: &str, repo_root: &Path) {
1349    if br == SCREENSHOTS_BRANCH {
1350        return;
1351    }
1352    let default_branch = worktree::detect_default_branch(repo_root).await;
1353    let _ = git_ops::git()
1354        .await
1355        .args([
1356            "-C",
1357            &repo_root.to_string_lossy(),
1358            "fetch",
1359            "origin",
1360            &default_branch,
1361            br,
1362        ])
1363        .output_with_context()
1364        .await;
1365
1366    delete_local_branch(task_id, br, repo_root).await;
1367
1368    let remote_ref = format!("refs/remotes/origin/{br}");
1369    let default_ref = format!("refs/remotes/origin/{default_branch}");
1370    match commits_outside(repo_root, &remote_ref, &[&default_ref]).await {
1371        Some(0) => {}
1372        Some(n) => {
1373            tracing::info!(task_id, branch = %br, unmerged = n, "remote branch has commits the default branch lacks, keeping it");
1374            return;
1375        }
1376        None => {
1377            tracing::debug!(task_id, branch = %br, "remote branch missing or unreadable, nothing to delete");
1378            return;
1379        }
1380    }
1381
1382    let remote_delete = git_ops::git()
1383        .await
1384        .args([
1385            "-C",
1386            &repo_root.to_string_lossy(),
1387            "-c",
1388            "lfs.locksverify=false",
1389            "push",
1390            "origin",
1391            "--delete",
1392            br,
1393        ])
1394        .output_with_context()
1395        .await;
1396
1397    match remote_delete {
1398        Ok(output) if output.status.success() => {
1399            tracing::info!(task_id, branch = %br, "remote branch deleted");
1400            git_ops::clear_push_lease(repo_root, br).await;
1401        }
1402        Ok(output) => {
1403            let stderr = String::from_utf8_lossy(&output.stderr);
1404            tracing::debug!(task_id, err = %stderr, "remote branch delete skipped");
1405        }
1406        Err(e) => {
1407            tracing::warn!(task_id, err = %e, "failed to delete remote branch");
1408        }
1409    }
1410}
1411
1412/// Returns `true` if any active tmux pane has its cwd inside `worktree`.
1413///
1414/// Uses `tmux list-panes -a -F '#{pane_current_path}'` to enumerate all
1415/// pane working directories across every session. If tmux is not running
1416/// (command fails), we conservatively return `false` so cleanup can proceed.
1417async fn is_worktree_in_active_session(worktree: &Path) -> bool {
1418    let output = Command::new("tmux")
1419        .args(["list-panes", "-a", "-F", "#{pane_current_path}"])
1420        .output()
1421        .await;
1422
1423    match output {
1424        Ok(o) if o.status.success() => {
1425            let worktree_str = worktree.to_string_lossy();
1426            String::from_utf8_lossy(&o.stdout).lines().any(|line| {
1427                let pane_path = line.trim();
1428                // Match exact path or any path that starts with worktree + separator.
1429                pane_path == worktree_str.as_ref()
1430                    || pane_path.starts_with(&format!("{worktree_str}/"))
1431            })
1432        }
1433        // tmux not running or no sessions — treat as safe to clean.
1434        _ => false,
1435    }
1436}
1437
1438/// Returns how many hours old the worktree directory is, based on its mtime.
1439///
1440/// Returns `None` if the directory does not exist or the mtime cannot be read.
1441async fn worktree_age_hours(worktree: &Path) -> Option<u64> {
1442    // Use tokio::fs to avoid blocking the async runtime when reading metadata.
1443    let metadata = fs::metadata(worktree).await.ok()?;
1444    let modified = metadata.modified().ok()?;
1445    let age = SystemTime::now().duration_since(modified).ok()?;
1446    Some(age.as_secs() / 3600)
1447}
1448
1449/// Resolve the repo root for an orphaned worktree and remove it.
1450///
1451/// Used by `orch service doctor --fix` and `orch service prune` to clean up worktrees that are
1452/// no longer owned by any task. Returns `true` if the worktree directory is gone
1453/// after the call.
1454pub(crate) async fn cleanup_orphaned_worktree(task_id: &str, wt: &Path) -> bool {
1455    let repo_root = match resolve_repo_root_for_orphaned_worktree(wt).await {
1456        Ok(root) => root,
1457        Err(e) => {
1458            tracing::warn!(
1459                worktree = %wt.display(),
1460                error = %e,
1461                "cannot resolve repo root for orphaned worktree"
1462            );
1463            return false;
1464        }
1465    };
1466    remove_worktree_and_branch(task_id, wt, None, Path::new(&repo_root), false).await
1467}
1468
1469/// Resolve the main git repository root for an orphaned worktree.
1470///
1471/// First tries to parse the `.git` file's `gitdir:` pointer (two levels up from
1472/// the pointer gives the bare repo root, which is the orch convention). Falls back
1473/// to looking up registered projects by directory name when the `.git` file is
1474/// absent or unparseable.
1475pub(crate) async fn resolve_repo_root_for_orphaned_worktree(wt: &Path) -> anyhow::Result<String> {
1476    let git_file = wt.join(".git");
1477    if fs::try_exists(&git_file).await.unwrap_or(false)
1478        && let Ok(content) = fs::read_to_string(&git_file).await
1479    {
1480        for line in content.lines() {
1481            if let Some(gitdir) = line.strip_prefix("gitdir: ") {
1482                // gitdir points to: <repo>/.git/worktrees/<name> (non-bare)
1483                //                or <repo>.git/worktrees/<name>   (bare)
1484                // Two levels up reaches the .git dir / bare repo root.
1485                let gitdir_path = PathBuf::from(gitdir);
1486                if let Some(repo_root) = gitdir_path.parent().and_then(|p| p.parent()) {
1487                    return Ok(repo_root.display().to_string());
1488                }
1489            }
1490        }
1491    }
1492
1493    // Fallback: look up registered projects by the project-name component of the
1494    // worktree path (format: ~/.orch/worktrees/<project>/<branch>).
1495    let worktrees_dir = home::worktrees_dir()?;
1496    if let Ok(relative) = wt.strip_prefix(&worktrees_dir)
1497        && let Some(project_name) = relative.components().next()
1498    {
1499        let project_name = project_name.as_os_str().to_string_lossy();
1500
1501        if let Ok(paths) = config::get_project_paths() {
1502            for path_str in &paths {
1503                let path = Path::new(path_str);
1504                if let Some(name) = path.file_name()
1505                    && name.to_string_lossy() == project_name
1506                {
1507                    return Ok(path_str.clone());
1508                }
1509            }
1510        }
1511
1512        // Try bare clone at ~/.orch/projects/<owner>/<project>.git
1513        let parts: Vec<&str> = project_name.split('/').collect();
1514        let bare = if parts.len() == 2 {
1515            home::projects_dir()
1516                .map(|d| d.join(parts[0]).join(format!("{}.git", parts[1])))
1517                .unwrap_or_default()
1518        } else {
1519            return Err(anyhow::anyhow!(
1520                "cannot determine owner for single-segment project name '{project_name}' — \
1521                     bare repo location is ambiguous; register the project with 'owner/repo' format"
1522            ));
1523        };
1524        if fs::try_exists(&bare).await.unwrap_or(false) {
1525            return Ok(bare.display().to_string());
1526        }
1527    }
1528
1529    anyhow::bail!(
1530        "cannot resolve repo root for orphaned worktree at {} — tried .git parsing and project lookup",
1531        wt.display()
1532    )
1533}
1534
1535/// Resolve the main git repository root from a worktree's .git file.
1536///
1537/// For cross-project worktree cleanup, we cannot rely on `resolve_repo_root` because
1538/// the worktree may belong to a different project than the current repo context.
1539/// Instead, we extract the repo root from the worktree's `.git` file which contains
1540/// a `gitdir:` pointer to the main repo's git directory.
1541pub async fn resolve_repo_root_from_worktree(wt: &Path) -> anyhow::Result<String> {
1542    // For worktrees, .git is a file (not a directory) containing gitdir path
1543    let git_file = wt.join(".git");
1544    if !fs::try_exists(&git_file).await.unwrap_or(false) {
1545        anyhow::bail!(".git file not found in worktree at {}", wt.display());
1546    }
1547
1548    let content = fs::read_to_string(&git_file)
1549        .await
1550        .map_err(|e| anyhow::anyhow!("failed to read .git file: {e}"))?;
1551
1552    // Parse gitdir line: "gitdir: /path/to/repo/.git/worktrees/<name>"
1553    for line in content.lines() {
1554        if let Some(gitdir) = line.strip_prefix("gitdir:") {
1555            let gitdir = gitdir.trim();
1556            // The main repo is the parent of the .git directory
1557            // gitdir points to: <repo>/.git/worktrees/<name>
1558            // We need to go up 3 levels: worktrees/<name> -> .git -> repo
1559            let gitdir_path = Path::new(gitdir);
1560            if let Some(repo_root) = gitdir_path
1561                .parent() // worktrees/<name>
1562                .and_then(|p| p.parent()) // .git
1563                .and_then(|p| p.parent())
1564            // repo root
1565            {
1566                return Ok(repo_root.display().to_string());
1567            }
1568        }
1569    }
1570
1571    anyhow::bail!(
1572        "cannot resolve repo root from worktree .git file at {}",
1573        wt.display()
1574    )
1575}
1576
1577/// Resolve the main git repository root path for a project.
1578///
1579/// Looks up the local project path from config, then verifies it's a git repo.
1580/// This avoids relying on cwd (which is undefined under launchd services).
1581pub(crate) async fn resolve_repo_root(repo: &str) -> anyhow::Result<String> {
1582    // Look up the local path from registered projects
1583    let paths = config::get_project_paths().unwrap_or_default();
1584    for path_str in &paths {
1585        let path = Path::new(path_str);
1586        let orch_yml = path.join(".orch.yml");
1587        let legacy = path.join(".orchestrator.yml");
1588        let config_file = if fs::try_exists(&orch_yml).await.unwrap_or(false) {
1589            orch_yml
1590        } else if fs::try_exists(&legacy).await.unwrap_or(false) {
1591            legacy
1592        } else {
1593            continue;
1594        };
1595        if let Ok(content) = fs::read_to_string(&config_file).await
1596            && let Ok(doc) = serde_norway::from_str::<serde_norway::Value>(&content)
1597            && let Some(r) = doc
1598                .get("gh")
1599                .and_then(|gh| gh.get("repo"))
1600                .and_then(|r| r.as_str())
1601            && r == repo
1602        {
1603            return Ok(path_str.clone());
1604        }
1605    }
1606
1607    // Fallback: try bare clone in ~/.orch/projects/<owner>/<repo>.git
1608    let parts: Vec<&str> = repo.split('/').collect();
1609    let bare = if parts.len() == 2 {
1610        home::projects_dir()
1611            .map(|d| d.join(parts[0]).join(format!("{}.git", parts[1])))
1612            .unwrap_or_default()
1613    } else {
1614        PathBuf::new()
1615    };
1616    if fs::try_exists(&bare).await.unwrap_or(false) {
1617        return Ok(bare.display().to_string());
1618    }
1619
1620    anyhow::bail!(
1621        "cannot find local path for project {repo} — \
1622         checked {} registered project(s) and bare clone at {}",
1623        paths.len(),
1624        bare.display()
1625    )
1626}
1627
1628/// Resolve the project directory for a given repo string.
1629///
1630/// Replicates the logic from `TaskRunner::resolve_project_dir()` without requiring
1631/// a Runner instance. Used by the orphan scanner to enumerate active worktrees.
1632fn resolve_project_dir_for_repo(repo: &str) -> anyhow::Result<PathBuf> {
1633    // Check PROJECT_DIR env var first
1634    if let Ok(dir) = env::var("PROJECT_DIR")
1635        && !dir.is_empty()
1636    {
1637        return Ok(PathBuf::from(dir));
1638    }
1639
1640    // Look up registered projects in config
1641    if let Ok(paths) = config::get_project_paths() {
1642        for path_str in &paths {
1643            let path = Path::new(path_str);
1644            let orch_yml = path.join(".orch.yml");
1645            let legacy = path.join(".orchestrator.yml");
1646            let config_file = if Path::new(&orch_yml).exists() {
1647                orch_yml
1648            } else if Path::new(&legacy).exists() {
1649                legacy
1650            } else {
1651                continue;
1652            };
1653            if let Ok(content) = std_fs::read_to_string(&config_file)
1654                && let Ok(doc) = serde_norway::from_str::<serde_norway::Value>(&content)
1655                && let Some(r) = doc
1656                    .get("gh")
1657                    .and_then(|gh| gh.get("repo"))
1658                    .and_then(|r| r.as_str())
1659                && r == repo
1660            {
1661                return Ok(path_str.clone().into());
1662            }
1663        }
1664    }
1665
1666    anyhow::bail!("project dir not found for repo: {repo}")
1667}
1668
1669/// Check for merged PRs and update task status accordingly.
1670///
1671/// Queries status:in_review and status:needs_review tasks, checks if their PR
1672/// is merged, and updates status to done if merged.
1673pub(crate) async fn check_merged_prs(
1674    backend: &Arc<dyn ExternalBackend>,
1675    repo: &str,
1676    store: &Arc<TaskStore>,
1677    task_manager: &Arc<TaskManager>,
1678    in_review_tasks: &[ReviewTaskSnapshot],
1679    needs_review_tasks: &[ReviewTaskSnapshot],
1680) -> anyhow::Result<()> {
1681    let mut branch_map: HashMap<String, String> = HashMap::new();
1682    let all_review_tasks: Vec<&ReviewTaskSnapshot> = in_review_tasks
1683        .iter()
1684        .chain(needs_review_tasks.iter())
1685        .collect();
1686
1687    for task in &all_review_tasks {
1688        if !task.stored.branch.is_empty() {
1689            branch_map.insert(task.external.id.0.clone(), task.stored.branch.clone());
1690        }
1691    }
1692
1693    tracing::debug!(
1694        count = all_review_tasks.len(),
1695        "checking review tasks for merged PRs"
1696    );
1697
1698    // Collect (task_id, branch) pairs — skip tasks with no branch recorded.
1699    // Use the pre-built branch map; fall back to store lookup only for tasks
1700    // not in the map (e.g. internal tasks, backend fallback path).
1701    let mut task_branches: Vec<(String, String)> = Vec::new();
1702    for task in all_review_tasks {
1703        let task_id = &task.external.id.0;
1704        let branch = if let Some(b) = branch_map.get(task_id) {
1705            b.clone()
1706        } else {
1707            match store::opt_store_get_task(&Some(Arc::clone(store)), repo, task_id)
1708                .await
1709                .map(|t| t.branch)
1710            {
1711                Some(b) if !b.is_empty() => b,
1712                _ => {
1713                    tracing::debug!(task_id, "no branch info, skipping PR check");
1714                    continue;
1715                }
1716            }
1717        };
1718        task_branches.push((task_id.clone(), branch));
1719    }
1720
1721    if task_branches.is_empty() {
1722        return Ok(());
1723    }
1724
1725    // Single GraphQL call for all branches — N REST calls → 1 GraphQL call.
1726    let branches: Vec<String> = task_branches.iter().map(|(_, b)| b.clone()).collect();
1727    let merged_map = match backend.batch_is_pr_merged(&branches).await {
1728        Ok(map) => map,
1729        Err(e) => {
1730            tracing::warn!(
1731                err = %e,
1732                branch_count = branches.len(),
1733                "batch PR merge check failed, retrying affected branches individually"
1734            );
1735            fallback_is_pr_merged_by_branch(backend, &branches).await
1736        }
1737    };
1738
1739    for (task_id, branch) in task_branches {
1740        let is_merged = merged_map.get(&branch).copied().unwrap_or(false);
1741        if !is_merged {
1742            continue;
1743        }
1744
1745        tracing::info!(task_id, branch = %branch, "PR merged, marking task complete");
1746
1747        let stored = store::opt_store_get_task(&Some(Arc::clone(store)), repo, &task_id).await;
1748        if let Some(ref stored) = stored {
1749            let external = store_task_to_external(stored);
1750            if ci_bootstrap::hold_after_merge(&external, store, repo).await {
1751                tracing::info!(
1752                    task_id,
1753                    branch = %branch,
1754                    "CI bootstrap PR merged, holding done until the ruleset exists"
1755                );
1756                if let Err(e) = cleanup_task_worktree(&task_id, repo, store).await {
1757                    tracing::warn!(task_id, err = %e, "worktree cleanup after merge failed");
1758                }
1759                continue;
1760            }
1761        }
1762
1763        let id = ExternalId(task_id.clone());
1764        if let Err(e) = task_manager.update_task_status(&id, Status::Done).await {
1765            tracing::warn!(task_id, err = %e, "failed to update status to done");
1766            continue;
1767        }
1768
1769        if let Err(e) = cleanup_task_worktree(&task_id, repo, store).await {
1770            tracing::warn!(task_id, err = %e, "worktree cleanup after merge failed");
1771        }
1772
1773        let comment = format!("PR merged, marking task complete{}", engine::orch_footer());
1774        if let Err(e) = backend.post_comment(&id, &comment).await {
1775            tracing::warn!(task_id, err = %e, "failed to post comment");
1776        }
1777    }
1778
1779    Ok(())
1780}
1781
1782async fn fallback_is_pr_merged_by_branch(
1783    backend: &Arc<dyn ExternalBackend>,
1784    branches: &[String],
1785) -> HashMap<String, bool> {
1786    let futs: Vec<_> = branches
1787        .iter()
1788        .map(|branch| {
1789            let backend = Arc::clone(backend);
1790            let branch = branch.clone();
1791            async move {
1792                match backend.is_pr_merged(&branch).await {
1793                    Ok(is_merged) => Some((branch, is_merged)),
1794                    Err(err) => {
1795                        tracing::warn!(
1796                            branch,
1797                            err = %err,
1798                            "fallback PR merge check failed for branch"
1799                        );
1800                        None
1801                    }
1802                }
1803            }
1804        })
1805        .collect();
1806
1807    future::join_all(futs).await.into_iter().flatten().collect()
1808}
1809
1810#[cfg(test)]
1811mod tests {
1812    use super::*;
1813    use crate::engine::tasks;
1814    use crate::store::{NewTask, TaskStore};
1815    use async_trait::async_trait;
1816    use std::collections::HashMap;
1817    use std::process;
1818    use std::sync::Mutex;
1819    use std::sync::atomic::{AtomicUsize, Ordering};
1820
1821    // worktree_age_hours
1822
1823    #[tokio::test]
1824    async fn worktree_age_hours_returns_none_for_missing_dir() {
1825        let tmp = tempfile::tempdir().unwrap();
1826        let missing = tmp.path().join("does-not-exist");
1827        assert!(worktree_age_hours(&missing).await.is_none());
1828    }
1829
1830    #[tokio::test]
1831    async fn worktree_age_hours_returns_some_for_existing_dir() {
1832        let tmp = tempfile::tempdir().unwrap();
1833        // A freshly created directory should have age ~0 hours.
1834        let age = worktree_age_hours(tmp.path()).await;
1835        assert!(age.is_some());
1836        assert_eq!(age.unwrap(), 0, "freshly created dir should have age 0h");
1837    }
1838
1839    // JanitorOptions::from_config defaults
1840
1841    #[test]
1842    fn janitor_options_default_values() {
1843        let opts = JanitorOptions::default();
1844        assert_eq!(opts.ttl_hours, 24);
1845        assert!(!opts.dry_run);
1846    }
1847
1848    // cleanup_task_worktree_with_opts TTL guard (skips if git missing)
1849
1850    /// Create a minimal git repo + worktree; return (tmp, worktree_dir).
1851    fn setup_test_repo() -> Option<(tempfile::TempDir, PathBuf)> {
1852        let tmp = tempfile::tempdir().ok()?;
1853
1854        // Create a real git repo (not bare) so we can add worktrees.
1855        let repo_dir = tmp.path().join("repo.git");
1856        std_fs::create_dir_all(&repo_dir).ok()?;
1857
1858        let init = process::Command::new("git")
1859            .args([
1860                "init",
1861                "--initial-branch=main",
1862                repo_dir
1863                    .to_str()
1864                    .expect("test repo path contains non-UTF-8 characters"),
1865            ])
1866            .output()
1867            .ok()?;
1868        if !init.status.success() {
1869            // Older git may not have --initial-branch; fall back.
1870            let _ = process::Command::new("git")
1871                .args([
1872                    "init",
1873                    repo_dir
1874                        .to_str()
1875                        .expect("test repo path contains non-UTF-8 characters"),
1876                ])
1877                .output()
1878                .ok()?;
1879        }
1880
1881        // Create an initial commit so the repo has a HEAD.
1882        let readme = repo_dir.join("README.md");
1883        std_fs::write(&readme, "# test").ok()?;
1884        process::Command::new("git")
1885            .args(["-C", repo_dir.to_str()?, "add", "."])
1886            .output()
1887            .ok()?;
1888        process::Command::new("git")
1889            .args([
1890                "-C",
1891                repo_dir.to_str()?,
1892                "-c",
1893                "user.email=test@test.com",
1894                "-c",
1895                "user.name=Test",
1896                "-c",
1897                "commit.gpgsign=false",
1898                "commit",
1899                "--allow-empty",
1900                "-m",
1901                "init",
1902            ])
1903            .output()
1904            .ok()?;
1905
1906        // Create a worktree branch.
1907        let wt_dir = tmp
1908            .path()
1909            .join("worktrees")
1910            .join("repo.git")
1911            .join("gh-issue-42-test");
1912        std_fs::create_dir_all(wt_dir.parent()?).ok()?;
1913        let wt_out = process::Command::new("git")
1914            .args([
1915                "-C",
1916                repo_dir.to_str()?,
1917                "worktree",
1918                "add",
1919                "-b",
1920                "gh-issue-42-test",
1921                wt_dir.to_str()?,
1922            ])
1923            .output()
1924            .ok()?;
1925
1926        if !wt_out.status.success() {
1927            return None;
1928        }
1929
1930        Some((tmp, wt_dir))
1931    }
1932
1933    #[tokio::test]
1934    async fn janitor_skips_young_worktree() {
1935        let Some((tmp, wt_dir)) = setup_test_repo() else {
1936            eprintln!("skipping test: git not available");
1937            return;
1938        };
1939
1940        // The worktree was just created → age is 0h < ttl_hours=24 → must be skipped.
1941        assert!(wt_dir.exists(), "worktree should exist before janitor runs");
1942
1943        // Run the TTL guard check directly (without a full engine).
1944        let age = worktree_age_hours(&wt_dir).await.unwrap_or(u64::MAX);
1945        assert!(
1946            age < 24,
1947            "freshly created worktree should be younger than 24h (age={age}h)"
1948        );
1949
1950        // Explicitly verify: with ttl_hours=24 the worktree stays.
1951        let opts = JanitorOptions {
1952            ttl_hours: 24,
1953            dry_run: true,
1954        };
1955        // Age < ttl → should skip (worktree_age_hours < opts.ttl_hours).
1956        assert!(age < opts.ttl_hours, "janitor should skip young worktree");
1957
1958        drop(tmp); // cleanup tempdir
1959    }
1960
1961    #[tokio::test]
1962    async fn janitor_eligible_when_ttl_zero() {
1963        let Some((tmp, wt_dir)) = setup_test_repo() else {
1964            eprintln!("skipping test: git not available");
1965            return;
1966        };
1967
1968        // With ttl_hours=0 even a brand-new worktree is eligible.
1969        let age = worktree_age_hours(&wt_dir).await.unwrap_or(u64::MAX);
1970        let opts = JanitorOptions {
1971            ttl_hours: 0,
1972            dry_run: true,
1973        };
1974        assert!(
1975            age >= opts.ttl_hours,
1976            "worktree should be eligible when ttl=0 (age={age}h)"
1977        );
1978
1979        drop(tmp);
1980    }
1981
1982    #[tokio::test]
1983    async fn dry_run_does_not_remove_worktree() {
1984        // Verify that with dry_run=true the worktree directory is NOT removed.
1985        let Some((tmp, wt_dir)) = setup_test_repo() else {
1986            eprintln!("skipping test: git not available");
1987            return;
1988        };
1989
1990        assert!(wt_dir.exists(), "worktree must exist before test");
1991
1992        // With dry_run=true and ttl_hours=0 the janitor would normally remove the
1993        // worktree, but dry_run prevents it.
1994        let opts = JanitorOptions {
1995            ttl_hours: 0,
1996            dry_run: true,
1997        };
1998
1999        // The janitor needs a repo_root to run git commands.  We point it at the
2000        // bare repo we created inside tmp (even though it's not registered in config).
2001        // Because resolve_repo_root will fail for our synthetic repo slug, we test the
2002        // inner helpers directly here.
2003        let age = worktree_age_hours(&wt_dir).await.unwrap_or(u64::MAX);
2004        let in_use = false; // no tmux running in tests
2005        let skip = age < opts.ttl_hours || in_use;
2006
2007        if !skip && opts.dry_run {
2008            // dry-run: log only, don't touch filesystem.
2009            tracing::debug!("[dry-run] would remove {}", wt_dir.display());
2010        } else if !skip {
2011            // This branch would do actual removal — but we're in dry_run mode above.
2012        }
2013
2014        // Worktree must still be there.
2015        assert!(
2016            wt_dir.exists(),
2017            "dry-run must not remove the worktree directory"
2018        );
2019
2020        drop(tmp);
2021    }
2022
2023    #[test]
2024    fn fallback_branch_path_construction() {
2025        // Verify the new fallback logic: when worktree path is absent but
2026        // branch is known, we should construct the path correctly.
2027        let tmp = tempfile::tempdir().unwrap();
2028        let wt_dir = tmp.path().join("myrepo").join("gh-issue-7-fix");
2029        std_fs::create_dir_all(&wt_dir).unwrap();
2030
2031        // Simulate: worktree=None, branch=Some("gh-issue-7-fix"), repo="owner/myrepo"
2032        let branch = "gh-issue-7-fix";
2033        let repo = "owner/myrepo";
2034        let worktrees_base = tmp.path();
2035
2036        let project = repo
2037            .rsplit('/')
2038            .next()
2039            .unwrap_or(repo)
2040            .trim_end_matches(".git");
2041        let candidate = worktrees_base.join(project).join(branch);
2042
2043        assert_eq!(candidate, wt_dir);
2044        assert!(
2045            candidate.exists(),
2046            "fallback path should point to existing dir"
2047        );
2048    }
2049
2050    /// A task with no worktree and no branch should return Ok(false) —
2051    /// nothing was cleaned, so git pull should NOT be triggered.
2052    #[tokio::test]
2053    async fn cleanup_returns_false_when_nothing_to_clean() {
2054        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2055
2056        // Create a done task with no worktree or branch fields set
2057        let id = store
2058            .create(&NewTask {
2059                external_id: Some("999".to_string()),
2060                repo: "owner/repo".to_string(),
2061                origin: "github".to_string(),
2062                title: "Already cleaned".to_string(),
2063                ..Default::default()
2064            })
2065            .await
2066            .unwrap();
2067        store.update_status(id, TaskStatus::Done).await.unwrap();
2068
2069        let opts = JanitorOptions {
2070            ttl_hours: 0,
2071            dry_run: false,
2072        };
2073
2074        let result = cleanup_task_worktree_with_opts("999", "owner/repo", &store, &opts).await;
2075        // resolve_repo_root will fail for synthetic repo — that's OK, the point is
2076        // that when nothing needs cleaning the function should not report true.
2077        // In production this means no git pull is triggered.
2078        if let Ok(cleaned) = result {
2079            assert!(
2080                !cleaned,
2081                "cleanup should return false when there is no worktree or branch to clean"
2082            );
2083        } else {
2084            // resolve_repo_root fails for test repo — acceptable.
2085            // The bug was that even with no worktree/branch, Ok(true) was returned.
2086        }
2087    }
2088
2089    /// When the stored worktree path no longer exists on disk and there is no
2090    /// branch recorded, cleanup must mark the task as cleaned so subsequent
2091    /// runs do not keep retrying (idempotency regression test for #1021).
2092    #[tokio::test]
2093    async fn cleanup_marks_cleaned_when_stored_path_is_gone() {
2094        let tmp = tempfile::tempdir().unwrap();
2095        // A path that does NOT exist on disk.
2096        let stale_wt = tmp.path().join("stale-worktree");
2097        assert!(!stale_wt.exists(), "stale path must not exist");
2098
2099        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2100
2101        let id = store
2102            .create(&NewTask {
2103                external_id: Some("1005".to_string()),
2104                repo: "owner/repo".to_string(),
2105                origin: "github".to_string(),
2106                title: "Stale worktree task".to_string(),
2107                ..Default::default()
2108            })
2109            .await
2110            .unwrap();
2111        store.update_status(id, TaskStatus::Done).await.unwrap();
2112        // Set the stale worktree path on the task.
2113        store
2114            .set_fields(
2115                id,
2116                &[("worktree", stale_wt.to_string_lossy().as_ref().into())],
2117            )
2118            .await
2119            .unwrap();
2120
2121        // Verify the task starts out NOT cleaned.
2122        let before = store.get(id).await.unwrap();
2123        assert!(!before.worktree_cleaned, "task should not be cleaned yet");
2124        assert!(
2125            !before.worktree.is_empty(),
2126            "task must have a worktree path set"
2127        );
2128
2129        let opts = JanitorOptions {
2130            ttl_hours: 0,
2131            dry_run: false,
2132        };
2133
2134        let result = cleanup_task_worktree_with_opts("1005", "owner/repo", &store, &opts).await;
2135
2136        // The function should succeed (Ok) — stale path is not an error.
2137        // It returns Ok(false) because there was nothing to actively remove.
2138        match result {
2139            Ok(_) => {}
2140            Err(e) => panic!("cleanup should not error for stale path: {e}"),
2141        }
2142
2143        // The task must now be marked cleaned so the next cycle skips it.
2144        let after = store.get(id).await.unwrap();
2145        assert!(
2146            after.worktree_cleaned,
2147            "task must be marked cleaned when stored worktree path is already gone"
2148        );
2149    }
2150
2151    /// `remove_worktree_and_branch` returns true when the worktree directory
2152    /// is successfully removed (regression test for #1143).
2153    #[tokio::test]
2154    async fn remove_worktree_returns_true_on_success() {
2155        let Some((tmp, wt_dir)) = setup_test_repo() else {
2156            eprintln!("skipping test: git not available");
2157            return;
2158        };
2159
2160        let repo_dir = tmp.path().join("repo.git");
2161        assert!(wt_dir.exists(), "worktree must exist before removal");
2162
2163        let removed =
2164            remove_worktree_and_branch("42", &wt_dir, Some("gh-issue-42-test"), &repo_dir, false)
2165                .await;
2166
2167        assert!(
2168            removed,
2169            "remove_worktree_and_branch should return true when directory is gone"
2170        );
2171        assert!(!wt_dir.exists(), "worktree directory should be removed");
2172    }
2173
2174    /// Successful worktree cleanup must still delete the local branch.
2175    /// This guards against early-return regressions in `remove_worktree_and_branch`.
2176    #[tokio::test]
2177    async fn remove_worktree_success_still_deletes_local_branch() {
2178        let Some((tmp, wt_dir)) = setup_test_repo() else {
2179            eprintln!("skipping test: git not available");
2180            return;
2181        };
2182
2183        let repo_dir = tmp.path().join("repo.git");
2184        let repo_dir_str = repo_dir
2185            .to_str()
2186            .expect("test repo path contains non-UTF-8 characters");
2187        let branch = "gh-issue-42-test";
2188
2189        let before = process::Command::new("git")
2190            .args([
2191                "-C",
2192                repo_dir_str,
2193                "show-ref",
2194                "--verify",
2195                "--quiet",
2196                &format!("refs/heads/{branch}"),
2197            ])
2198            .status()
2199            .unwrap();
2200        assert!(
2201            before.success(),
2202            "test setup must create local branch before cleanup"
2203        );
2204
2205        let removed =
2206            remove_worktree_and_branch("42", &wt_dir, Some(branch), &repo_dir, false).await;
2207        assert!(removed, "worktree should be removed");
2208
2209        let after = process::Command::new("git")
2210            .args([
2211                "-C",
2212                repo_dir_str,
2213                "show-ref",
2214                "--verify",
2215                "--quiet",
2216                &format!("refs/heads/{branch}"),
2217            ])
2218            .status()
2219            .unwrap();
2220        assert!(
2221            !after.success(),
2222            "local branch should be deleted after successful worktree cleanup"
2223        );
2224    }
2225
2226    /// `remove_worktree_and_branch` returns false when the worktree directory
2227    /// cannot be removed (regression test for #1143 — prevents orphaned worktrees
2228    /// from being permanently marked as cleaned).
2229    #[tokio::test]
2230    async fn remove_worktree_returns_false_when_dir_persists() {
2231        let tmp = tempfile::tempdir().unwrap();
2232        // Create a plain directory (not a real git worktree) — git will fail
2233        // to remove it, so the directory stays on disk.
2234        let fake_wt = tmp.path().join("not-a-worktree");
2235        std_fs::create_dir_all(&fake_wt).unwrap();
2236
2237        // repo_root doesn't matter much — it just needs to exist for the -C flag.
2238        // Using tmp itself; git commands will fail but that's the point.
2239        let removed = remove_worktree_and_branch("99", &fake_wt, None, tmp.path(), false).await;
2240
2241        // The directory still exists because git couldn't remove it.
2242        // Before the fix, this would still have been marked as cleaned.
2243        assert!(
2244            !removed,
2245            "remove_worktree_and_branch should return false when directory still exists"
2246        );
2247        assert!(fake_wt.exists(), "directory should still be on disk");
2248    }
2249
2250    struct MergeCheckMockBackend {
2251        batch_result: Mutex<anyhow::Result<HashMap<String, bool>>>,
2252        single_results: HashMap<String, anyhow::Result<bool>>,
2253        posted_comments: Arc<Mutex<Vec<(String, String)>>>,
2254        single_checked_branches: Arc<Mutex<Vec<String>>>,
2255    }
2256
2257    impl MergeCheckMockBackend {
2258        fn with_batch_error(
2259            error: anyhow::Error,
2260            single_results: HashMap<String, anyhow::Result<bool>>,
2261        ) -> Self {
2262            Self {
2263                batch_result: Mutex::new(Err(error)),
2264                single_results,
2265                posted_comments: Arc::new(Mutex::new(Vec::new())),
2266                single_checked_branches: Arc::new(Mutex::new(Vec::new())),
2267            }
2268        }
2269    }
2270
2271    #[async_trait]
2272    impl ExternalBackend for MergeCheckMockBackend {
2273        fn name(&self) -> &'static str {
2274            "mock"
2275        }
2276
2277        async fn create_task(
2278            &self,
2279            _title: &str,
2280            _body: &str,
2281            _labels: &[String],
2282        ) -> anyhow::Result<ExternalId> {
2283            anyhow::bail!("not implemented")
2284        }
2285
2286        async fn get_task(&self, _id: &ExternalId) -> anyhow::Result<ExternalTask> {
2287            anyhow::bail!("not implemented")
2288        }
2289
2290        async fn list_by_status(&self, _status: Status) -> anyhow::Result<Vec<ExternalTask>> {
2291            Ok(vec![])
2292        }
2293
2294        async fn list_routable(&self) -> anyhow::Result<Vec<ExternalTask>> {
2295            Ok(vec![])
2296        }
2297
2298        async fn post_comment(&self, id: &ExternalId, body: &str) -> anyhow::Result<()> {
2299            self.posted_comments
2300                .lock()
2301                .unwrap()
2302                .push((id.0.clone(), body.to_string()));
2303            Ok(())
2304        }
2305
2306        async fn set_labels(&self, _id: &ExternalId, _labels: &[String]) -> anyhow::Result<()> {
2307            Ok(())
2308        }
2309
2310        async fn remove_label(&self, _id: &ExternalId, _label: &str) -> anyhow::Result<()> {
2311            Ok(())
2312        }
2313
2314        async fn get_sub_issues(&self, _id: &ExternalId) -> anyhow::Result<Vec<ExternalId>> {
2315            Ok(vec![])
2316        }
2317
2318        async fn create_sub_task(
2319            &self,
2320            _parent: &ExternalId,
2321            _title: &str,
2322            _body: &str,
2323            _labels: &[String],
2324        ) -> anyhow::Result<ExternalId> {
2325            anyhow::bail!("not implemented")
2326        }
2327
2328        async fn health_check(&self) -> anyhow::Result<()> {
2329            Ok(())
2330        }
2331
2332        async fn is_pr_merged(&self, branch: &str) -> anyhow::Result<bool> {
2333            self.single_checked_branches
2334                .lock()
2335                .unwrap()
2336                .push(branch.to_string());
2337            match self.single_results.get(branch) {
2338                Some(Ok(is_merged)) => Ok(*is_merged),
2339                Some(Err(err)) => Err(anyhow::anyhow!(err.to_string())),
2340                None => Ok(false),
2341            }
2342        }
2343
2344        async fn batch_is_pr_merged(
2345            &self,
2346            _branches: &[String],
2347        ) -> anyhow::Result<HashMap<String, bool>> {
2348            match &*self.batch_result.lock().unwrap() {
2349                Ok(map) => Ok(map.clone()),
2350                Err(err) => Err(anyhow::anyhow!(err.to_string())),
2351            }
2352        }
2353    }
2354
2355    #[tokio::test]
2356    async fn check_merged_prs_falls_back_to_single_branch_checks_on_batch_error() {
2357        let single_results = HashMap::from([
2358            ("branch-1".to_string(), Ok(true)),
2359            ("branch-2".to_string(), Ok(false)),
2360        ]);
2361        let backend = Arc::new(MergeCheckMockBackend::with_batch_error(
2362            anyhow::anyhow!("missing /data/repository in GraphQL response: no error details"),
2363            single_results,
2364        ));
2365        let backend_dyn: Arc<dyn ExternalBackend> = Arc::<MergeCheckMockBackend>::clone(&backend);
2366
2367        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2368        let task1 = store
2369            .create(&NewTask {
2370                external_id: Some("101".to_string()),
2371                repo: "owner/repo".to_string(),
2372                origin: "github".to_string(),
2373                title: "Task 101".to_string(),
2374                ..Default::default()
2375            })
2376            .await
2377            .unwrap();
2378        store
2379            .update_status(task1, TaskStatus::NeedsReview)
2380            .await
2381            .unwrap();
2382        store
2383            .set_fields(task1, &[("branch", serde_json::json!("branch-1"))])
2384            .await
2385            .unwrap();
2386        // No .git file, so cleanup force-removes the directory
2387        let wt_dir = tempfile::tempdir().unwrap();
2388        let wt_path = wt_dir.path().join("branch-1");
2389        std_fs::create_dir_all(&wt_path).unwrap();
2390        store
2391            .set_fields(
2392                task1,
2393                &[("worktree", serde_json::json!(wt_path.to_string_lossy()))],
2394            )
2395            .await
2396            .unwrap();
2397
2398        let task2 = store
2399            .create(&NewTask {
2400                external_id: Some("102".to_string()),
2401                repo: "owner/repo".to_string(),
2402                origin: "github".to_string(),
2403                title: "Task 102".to_string(),
2404                ..Default::default()
2405            })
2406            .await
2407            .unwrap();
2408        store
2409            .update_status(task2, TaskStatus::InReview)
2410            .await
2411            .unwrap();
2412        store
2413            .set_fields(task2, &[("branch", serde_json::json!("branch-2"))])
2414            .await
2415            .unwrap();
2416
2417        let task_manager = Arc::new(TaskManager::with_store(
2418            Arc::clone(&backend_dyn),
2419            Arc::clone(&store),
2420            "owner/repo".to_string(),
2421        ));
2422
2423        let in_review_tasks = store
2424            .list_by_status("owner/repo", TaskStatus::InReview)
2425            .await
2426            .unwrap()
2427            .into_iter()
2428            .map(|stored| ReviewTaskSnapshot {
2429                external: tasks::store_task_to_external(&stored),
2430                stored,
2431            })
2432            .collect::<Vec<_>>();
2433        let needs_review_tasks = store
2434            .list_by_status("owner/repo", TaskStatus::NeedsReview)
2435            .await
2436            .unwrap()
2437            .into_iter()
2438            .map(|stored| ReviewTaskSnapshot {
2439                external: tasks::store_task_to_external(&stored),
2440                stored,
2441            })
2442            .collect::<Vec<_>>();
2443
2444        check_merged_prs(
2445            &backend_dyn,
2446            "owner/repo",
2447            &store,
2448            &task_manager,
2449            &in_review_tasks,
2450            &needs_review_tasks,
2451        )
2452        .await
2453        .unwrap();
2454
2455        let checked = backend.single_checked_branches.lock().unwrap().clone();
2456        assert_eq!(
2457            checked,
2458            vec!["branch-2".to_string(), "branch-1".to_string()]
2459        );
2460
2461        let updated_task1 = store.get(task1).await.unwrap();
2462        let updated_task2 = store.get(task2).await.unwrap();
2463        assert_eq!(updated_task1.status, TaskStatus::Done);
2464        assert!(updated_task1.worktree_cleaned);
2465        assert!(!wt_path.exists());
2466        assert!(!updated_task2.worktree_cleaned);
2467        assert_eq!(updated_task2.status, TaskStatus::InReview);
2468
2469        let comments = backend.posted_comments.lock().unwrap().clone();
2470        assert_eq!(comments.len(), 1);
2471        assert_eq!(comments[0].0, "101");
2472        assert!(comments[0].1.contains("PR merged, marking task complete"));
2473    }
2474
2475    struct ReconciliationMockBackend {
2476        reconciliation_calls: Arc<AtomicUsize>,
2477    }
2478
2479    #[async_trait]
2480    impl ExternalBackend for ReconciliationMockBackend {
2481        fn name(&self) -> &'static str {
2482            "mock"
2483        }
2484
2485        async fn create_task(
2486            &self,
2487            _title: &str,
2488            _body: &str,
2489            _labels: &[String],
2490        ) -> anyhow::Result<ExternalId> {
2491            anyhow::bail!("not implemented")
2492        }
2493
2494        async fn get_task(&self, _id: &ExternalId) -> anyhow::Result<ExternalTask> {
2495            anyhow::bail!("not implemented")
2496        }
2497
2498        async fn list_by_status(&self, _status: Status) -> anyhow::Result<Vec<ExternalTask>> {
2499            Ok(vec![])
2500        }
2501
2502        async fn list_reconciliation_candidates(&self) -> anyhow::Result<Vec<ExternalTask>> {
2503            self.reconciliation_calls.fetch_add(1, Ordering::SeqCst);
2504            Ok(vec![])
2505        }
2506
2507        async fn list_routable(&self) -> anyhow::Result<Vec<ExternalTask>> {
2508            Ok(vec![])
2509        }
2510
2511        async fn post_comment(&self, _id: &ExternalId, _body: &str) -> anyhow::Result<()> {
2512            Ok(())
2513        }
2514
2515        async fn set_labels(&self, _id: &ExternalId, _labels: &[String]) -> anyhow::Result<()> {
2516            Ok(())
2517        }
2518
2519        async fn remove_label(&self, _id: &ExternalId, _label: &str) -> anyhow::Result<()> {
2520            Ok(())
2521        }
2522
2523        async fn get_sub_issues(&self, _id: &ExternalId) -> anyhow::Result<Vec<ExternalId>> {
2524            Ok(vec![])
2525        }
2526
2527        async fn create_sub_task(
2528            &self,
2529            _parent: &ExternalId,
2530            _title: &str,
2531            _body: &str,
2532            _labels: &[String],
2533        ) -> anyhow::Result<ExternalId> {
2534            anyhow::bail!("not implemented")
2535        }
2536
2537        async fn health_check(&self) -> anyhow::Result<()> {
2538            Ok(())
2539        }
2540    }
2541
2542    #[tokio::test]
2543    async fn cleanup_reconciliation_is_rate_limited_per_repo() {
2544        let repo = "owner/repo-cleanup-rate-limit";
2545        RECONCILIATION_LAST_RUN.remove(repo);
2546
2547        let backend = Arc::new(ReconciliationMockBackend {
2548            reconciliation_calls: Arc::new(AtomicUsize::new(0)),
2549        });
2550        let backend_dyn: Arc<dyn ExternalBackend> =
2551            Arc::<ReconciliationMockBackend>::clone(&backend);
2552        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2553        let task_manager = Arc::new(TaskManager::with_store(
2554            Arc::clone(&backend_dyn),
2555            Arc::clone(&store),
2556            repo.to_string(),
2557        ));
2558        let opts = JanitorOptions {
2559            ttl_hours: 0,
2560            dry_run: true,
2561        };
2562
2563        cleanup_done_worktrees_with_opts(&backend_dyn, repo, &task_manager, &store, &opts)
2564            .await
2565            .unwrap();
2566        cleanup_done_worktrees_with_opts(&backend_dyn, repo, &task_manager, &store, &opts)
2567            .await
2568            .unwrap();
2569
2570        assert_eq!(
2571            backend.reconciliation_calls.load(Ordering::SeqCst),
2572            1,
2573            "cleanup should not re-list reconciliation candidates on every sync pass"
2574        );
2575
2576        RECONCILIATION_LAST_RUN.remove(repo);
2577    }
2578
2579    /// Cleanup only collects done tasks. A blocked task waits on a person and resumes from its
2580    /// worktree and branch, so it must not even be considered (#3781)
2581    #[tokio::test]
2582    async fn cleanup_skips_blocked_internal_tasks_and_cleans_done_ones() {
2583        let repo = "owner/repo-cleanup-blocked";
2584        RECONCILIATION_LAST_RUN.remove(repo);
2585        let backend_dyn: Arc<dyn ExternalBackend> = Arc::new(ReconciliationMockBackend {
2586            reconciliation_calls: Arc::new(AtomicUsize::new(0)),
2587        });
2588        let store = Arc::new(TaskStore::open_memory().await.unwrap());
2589        let task_manager = Arc::new(TaskManager::with_store(
2590            Arc::clone(&backend_dyn),
2591            Arc::clone(&store),
2592            repo.to_string(),
2593        ));
2594
2595        let mut ids = Vec::new();
2596        for (title, status) in [("blocked", TaskStatus::Blocked), ("done", TaskStatus::Done)] {
2597            let id = store
2598                .create_internal(repo, title, "", "test", title, None)
2599                .await
2600                .unwrap();
2601            store
2602                .set_fields(
2603                    id,
2604                    &[
2605                        (
2606                            "worktree",
2607                            serde_json::json!(format!("/nonexistent/{title}")),
2608                        ),
2609                        ("branch", serde_json::json!("")),
2610                    ],
2611                )
2612                .await
2613                .unwrap();
2614            store.update_status(id, status).await.unwrap();
2615            ids.push(id);
2616        }
2617
2618        let opts = JanitorOptions {
2619            ttl_hours: 0,
2620            dry_run: false,
2621        };
2622        cleanup_done_worktrees_with_opts(&backend_dyn, repo, &task_manager, &store, &opts)
2623            .await
2624            .unwrap();
2625
2626        assert!(
2627            !store.get(ids[0]).await.unwrap().worktree_cleaned,
2628            "blocked task must not be considered for cleanup"
2629        );
2630        assert!(
2631            store.get(ids[1]).await.unwrap().worktree_cleaned,
2632            "done task is cleaned, which proves the pass ran"
2633        );
2634        RECONCILIATION_LAST_RUN.remove(repo);
2635    }
2636
2637    fn git_ok(dir: &Path, args: &[&str]) -> String {
2638        let out = process::Command::new("git")
2639            .args([
2640                "-c",
2641                "commit.gpgsign=false",
2642                "-c",
2643                "user.name=T",
2644                "-c",
2645                "user.email=t@t",
2646            ])
2647            .args(args)
2648            .current_dir(dir)
2649            .output()
2650            .unwrap();
2651        assert!(
2652            out.status.success(),
2653            "git {args:?}: {}",
2654            String::from_utf8_lossy(&out.stderr)
2655        );
2656        String::from_utf8_lossy(&out.stdout).to_string()
2657    }
2658
2659    /// A clone of a bare origin with main pushed and branch `feat` one commit ahead
2660    fn origin_and_clone_with_feat() -> (tempfile::TempDir, PathBuf) {
2661        let tmp = tempfile::tempdir().unwrap();
2662        let bare = tmp.path().join("origin.git");
2663        std_fs::create_dir_all(&bare).unwrap();
2664        git_ok(&bare, &["init", "--bare", "-b", "main"]);
2665        git_ok(tmp.path(), &["clone", bare.to_str().unwrap(), "clone"]);
2666        let clone = tmp.path().join("clone");
2667        std_fs::write(clone.join("base.txt"), "base").unwrap();
2668        git_ok(&clone, &["add", "."]);
2669        git_ok(&clone, &["commit", "-m", "base"]);
2670        git_ok(&clone, &["push", "origin", "main"]);
2671        git_ok(&clone, &["checkout", "-b", "feat"]);
2672        std_fs::write(clone.join("work.txt"), "work").unwrap();
2673        git_ok(&clone, &["add", "."]);
2674        git_ok(&clone, &["commit", "-m", "work"]);
2675        git_ok(&clone, &["checkout", "main"]);
2676        (tmp, clone)
2677    }
2678
2679    fn remote_has(clone: &Path, branch: &str) -> bool {
2680        !git_ok(clone, &["ls-remote", "--heads", "origin", branch])
2681            .trim()
2682            .is_empty()
2683    }
2684
2685    fn local_has(clone: &Path, branch: &str) -> bool {
2686        process::Command::new("git")
2687            .args([
2688                "show-ref",
2689                "--verify",
2690                "--quiet",
2691                &format!("refs/heads/{branch}"),
2692            ])
2693            .current_dir(clone)
2694            .status()
2695            .unwrap()
2696            .success()
2697    }
2698
2699    /// Deleting a pushed branch whose commits main lacks would destroy the only copy of the
2700    /// work, as startup cleanup did to a blocked task (#3781)
2701    #[tokio::test]
2702    async fn delete_branches_keeps_an_unmerged_remote_branch() {
2703        let (_tmp, clone) = origin_and_clone_with_feat();
2704        git_ok(&clone, &["push", "origin", "feat"]);
2705
2706        delete_branches("1", "feat", &clone).await;
2707
2708        assert!(
2709            remote_has(&clone, "feat"),
2710            "unmerged remote branch must survive"
2711        );
2712        assert!(
2713            !local_has(&clone, "feat"),
2714            "local copy is on origin, so it can go"
2715        );
2716    }
2717
2718    #[tokio::test]
2719    async fn delete_branches_leaves_the_screenshots_branch_alone() {
2720        let (_tmp, clone) = origin_and_clone_with_feat();
2721        git_ok(&clone, &["branch", SCREENSHOTS_BRANCH, "feat"]);
2722        git_ok(&clone, &["push", "origin", SCREENSHOTS_BRANCH]);
2723        git_ok(&clone, &["merge", "--ff-only", "feat"]);
2724        git_ok(&clone, &["push", "origin", "main"]);
2725
2726        delete_branches("1", SCREENSHOTS_BRANCH, &clone).await;
2727
2728        assert!(remote_has(&clone, SCREENSHOTS_BRANCH));
2729        assert!(local_has(&clone, SCREENSHOTS_BRANCH));
2730    }
2731
2732    #[tokio::test]
2733    async fn delete_branches_removes_a_branch_main_already_contains() {
2734        let (_tmp, clone) = origin_and_clone_with_feat();
2735        git_ok(&clone, &["push", "origin", "feat"]);
2736        git_ok(&clone, &["merge", "--ff-only", "feat"]);
2737        git_ok(&clone, &["push", "origin", "main"]);
2738
2739        delete_branches("1", "feat", &clone).await;
2740
2741        assert!(!remote_has(&clone, "feat"));
2742        assert!(!local_has(&clone, "feat"));
2743    }
2744
2745    fn add_worktree(clone: &Path, tmp: &Path, branch: &str) -> PathBuf {
2746        let wt = tmp.join("wt");
2747        git_ok(clone, &["worktree", "add", wt.to_str().unwrap(), branch]);
2748        wt
2749    }
2750
2751    #[tokio::test]
2752    async fn cleanup_pushes_unpushed_commits_to_a_rescue_branch() {
2753        let (tmp, clone) = origin_and_clone_with_feat();
2754        let wt = add_worktree(&clone, tmp.path(), "feat");
2755
2756        let sha = git_ok(&clone, &["rev-parse", "--short", "feat"]);
2757        let sha = sha.trim();
2758        match rescue_unpushed_work("1", Some(&wt), "feat", &clone).await {
2759            Rescue::Pushed { branch, commits } => {
2760                assert_eq!(branch, "rescue/feat");
2761                assert!(rescue_comment(&branch, &commits).contains(sha));
2762            }
2763            other => panic!("expected a rescue push, got {other:?}"),
2764        }
2765
2766        let removed = remove_worktree_and_branch("1", &wt, Some("feat"), &clone, false).await;
2767
2768        assert!(removed);
2769        assert!(remote_has(&clone, "rescue/feat"));
2770        assert!(!local_has(&clone, "feat"));
2771    }
2772
2773    #[tokio::test]
2774    async fn cleanup_commits_uncommitted_changes_before_the_rescue() {
2775        let (tmp, clone) = origin_and_clone_with_feat();
2776        git_ok(&clone, &["push", "origin", "feat"]);
2777        let wt = add_worktree(&clone, tmp.path(), "feat");
2778        std_fs::write(wt.join("dirty.txt"), "dirty").unwrap();
2779
2780        let removed = remove_worktree_and_branch("1", &wt, Some("feat"), &clone, false).await;
2781
2782        assert!(removed);
2783        assert!(remote_has(&clone, "rescue/feat"));
2784    }
2785
2786    #[tokio::test]
2787    async fn cleanup_makes_no_rescue_branch_for_a_pushed_branch() {
2788        let (tmp, clone) = origin_and_clone_with_feat();
2789        git_ok(&clone, &["push", "origin", "feat"]);
2790        let wt = add_worktree(&clone, tmp.path(), "feat");
2791
2792        let removed = remove_worktree_and_branch("1", &wt, Some("feat"), &clone, false).await;
2793
2794        assert!(removed);
2795        assert!(!remote_has(&clone, "rescue/feat"));
2796        assert!(!local_has(&clone, "feat"));
2797    }
2798
2799    #[tokio::test]
2800    async fn rescue_skips_a_branch_on_the_remote_without_a_tracking_ref() {
2801        let (_tmp, clone) = origin_and_clone_with_feat();
2802        git_ok(&clone, &["push", "origin", "feat"]);
2803        git_ok(&clone, &["update-ref", "-d", "refs/remotes/origin/feat"]);
2804
2805        let rescue = rescue_unpushed_work("1", None, "feat", &clone).await;
2806
2807        assert_eq!(rescue, Rescue::Clean);
2808        assert!(!remote_has(&clone, "rescue/feat"));
2809    }
2810
2811    #[tokio::test]
2812    async fn merged_pr_cleanup_pushes_no_rescue_branch() {
2813        let (tmp, clone) = origin_and_clone_with_feat();
2814        let wt = add_worktree(&clone, tmp.path(), "feat");
2815
2816        let removed =
2817            remove_worktree_and_branch_for("1", &wt, Some("feat"), &clone, false, true).await;
2818
2819        assert!(removed);
2820        assert!(!wt.exists());
2821        assert!(!remote_has(&clone, "rescue/feat"));
2822    }
2823
2824    #[tokio::test]
2825    async fn unmerged_cleanup_still_pushes_a_rescue_branch() {
2826        let (tmp, clone) = origin_and_clone_with_feat();
2827        let wt = add_worktree(&clone, tmp.path(), "feat");
2828
2829        let removed =
2830            remove_worktree_and_branch_for("1", &wt, Some("feat"), &clone, false, false).await;
2831
2832        assert!(removed);
2833        assert!(remote_has(&clone, "rescue/feat"));
2834    }
2835
2836    #[tokio::test]
2837    async fn rescue_ignores_a_remote_tip_missing_locally() {
2838        let (tmp, clone) = origin_and_clone_with_feat();
2839        git_ok(&clone, &["push", "origin", "feat"]);
2840        let other = tmp.path().join("other");
2841        git_ok(
2842            tmp.path(),
2843            &["clone", "-b", "feat", "origin.git", other.to_str().unwrap()],
2844        );
2845        std_fs::write(other.join("extra.txt"), "extra").unwrap();
2846        git_ok(&other, &["add", "."]);
2847        git_ok(&other, &["commit", "-m", "extra"]);
2848        git_ok(&other, &["push", "origin", "feat"]);
2849
2850        let rescue = rescue_unpushed_work("1", None, "feat", &clone).await;
2851
2852        assert_ne!(rescue, Rescue::Failed);
2853    }
2854
2855    #[tokio::test]
2856    async fn cleanup_keeps_worktree_and_branch_when_the_rescue_push_fails() {
2857        let (tmp, clone) = origin_and_clone_with_feat();
2858        let wt = add_worktree(&clone, tmp.path(), "feat");
2859        git_ok(
2860            &clone,
2861            &["remote", "set-url", "origin", "/nonexistent/origin.git"],
2862        );
2863
2864        let removed = remove_worktree_and_branch("1", &wt, Some("feat"), &clone, false).await;
2865
2866        assert!(!removed);
2867        assert!(wt.exists());
2868        assert!(local_has(&clone, "feat"));
2869    }
2870
2871    #[tokio::test]
2872    async fn delete_branches_keeps_a_local_branch_with_unpushed_commits() {
2873        let (_tmp, clone) = origin_and_clone_with_feat();
2874
2875        delete_branches("1", "feat", &clone).await;
2876
2877        assert!(
2878            local_has(&clone, "feat"),
2879            "unpushed commits must not be deleted"
2880        );
2881    }
2882}