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