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