From d6709cc2cbc99f88f8d88b252a884b81edeabacc Mon Sep 17 00:00:00 2001 From: Agent Date: Thu, 2 Apr 2026 05:31:26 +0000 Subject: [PATCH] fix: dev-agent failure cleanup should preserve remote branch and PR for debugging --- dev/dev-agent.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/dev/dev-agent.sh b/dev/dev-agent.sh index 984707d..c401efd 100755 --- a/dev/dev-agent.sh +++ b/dev/dev-agent.sh @@ -41,7 +41,7 @@ REPO_ROOT="${PROJECT_REPO_ROOT}" LOCKFILE="/tmp/dev-agent-${PROJECT_NAME:-default}.lock" STATUSFILE="/tmp/dev-agent-status-${PROJECT_NAME:-default}" -BRANCH="fix/issue-${ISSUE}" +BRANCH="fix/issue-${ISSUE}" # Default; will be updated after FORGE_REMOTE is known WORKTREE="/tmp/${PROJECT_NAME}-worktree-${ISSUE}" SID_FILE="/tmp/dev-session-${PROJECT_NAME}-${ISSUE}.sid" PREFLIGHT_RESULT="/tmp/dev-agent-preflight.json" @@ -263,6 +263,14 @@ FORGE_REMOTE="${FORGE_REMOTE:-origin}" export FORGE_REMOTE log "forge remote: ${FORGE_REMOTE}" +# Generate unique branch name per attempt to avoid collision with failed attempts +# First attempt: fix/issue-N, subsequent: fix/issue-N-1, fix/issue-N-2, etc. +ATTEMPT=$(git ls-remote --heads "$FORGE_REMOTE" "fix/issue-${ISSUE}*" 2>/dev/null | wc -l) +if [ "$ATTEMPT" -gt 0 ]; then + BRANCH="fix/issue-${ISSUE}-${ATTEMPT}" +fi +log "using branch: ${BRANCH}" + if [ "$RECOVERY_MODE" = true ]; then if ! worktree_recover "$WORKTREE" "$BRANCH" "$FORGE_REMOTE"; then log "ERROR: worktree recovery failed" @@ -575,11 +583,8 @@ 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 + # Cleanup on failure: preserve remote branch and PR for debugging, clean up local worktree + # Remote state (PR and branch) stays open for inspection of CI logs and review comments worktree_cleanup "$WORKTREE" rm -f "$SID_FILE" "$IMPL_SUMMARY_FILE" CLAIMED=false