From e9a4fc7b801c4980a9a1ee9fa06ef15a87a82e85 Mon Sep 17 00:00:00 2001 From: Agent Date: Wed, 1 Apr 2026 15:12:45 +0000 Subject: [PATCH] fix: bug: dev-agent does not clean up branch/worktree on CI exhausted or block (#115) --- dev/dev-agent.sh | 7 +++++++ lib/pr-lifecycle.sh | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/dev/dev-agent.sh b/dev/dev-agent.sh index 93acf17..984707d 100755 --- a/dev/dev-agent.sh +++ b/dev/dev-agent.sh @@ -575,6 +575,13 @@ else outcome="blocked_${_PR_WALK_EXIT_REASON:-agent_failed}" profile_write_journal "$ISSUE" "$ISSUE_TITLE" "$outcome" "$FILES_CHANGED" || true + # Cleanup on failure: close PR, delete remote branch, clean up worktree + if [ -n "$PR_NUMBER" ]; then + pr_close "$PR_NUMBER" + fi + git push "$FORGE_REMOTE" --delete "$BRANCH" 2>/dev/null || true + worktree_cleanup "$WORKTREE" + rm -f "$SID_FILE" "$IMPL_SUMMARY_FILE" CLAIMED=false fi diff --git a/lib/pr-lifecycle.sh b/lib/pr-lifecycle.sh index 0ea5125..76d8fd8 100644 --- a/lib/pr-lifecycle.sh +++ b/lib/pr-lifecycle.sh @@ -348,6 +348,22 @@ pr_is_merged() { [ "$merged" = "true" ] } +# --------------------------------------------------------------------------- +# pr_close — Close a PR via forge API. +# Args: pr_number +# Returns: 0=closed, 1=error +# --------------------------------------------------------------------------- +pr_close() { + local pr_num="$1" + + _prl_log "closing PR #${pr_num}" + curl -sf -X PATCH \ + -H "Authorization: token ${FORGE_TOKEN}" \ + -H "Content-Type: application/json" \ + "${FORGE_API}/pulls/${pr_num}" \ + -d '{"state":"closed"}' >/dev/null 2>&1 || true +} + # --------------------------------------------------------------------------- # pr_walk_to_merge — Walk a PR through CI, review, and merge. #