fix: preserve agentic branch when intermediate commits exist
_finalize_worktree was returning None and deleting the branch when the final commit was empty, even though _commit_iteration had already committed changes during the pipeline. Now checks git log for any commits on the branch before deciding to clean up. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -263,8 +263,6 @@ def _finalize_worktree(
|
|||||||
)
|
)
|
||||||
if committed:
|
if committed:
|
||||||
logger.info(" Agentic changes committed on branch: %s", branch_name)
|
logger.info(" Agentic changes committed on branch: %s", branch_name)
|
||||||
else:
|
|
||||||
logger.warning(" No agentic changes to commit (empty diff)")
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.warning(" Failed to commit agentic changes", exc_info=True)
|
logger.warning(" Failed to commit agentic changes", exc_info=True)
|
||||||
|
|
||||||
@@ -273,25 +271,36 @@ def _finalize_worktree(
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.warning("Failed to clean up worktree: %s", worktree_path)
|
logger.warning("Failed to clean up worktree: %s", worktree_path)
|
||||||
|
|
||||||
# Check if branch has any commits beyond the base — if not, delete it
|
# Check if branch has any commits beyond the base (including intermediate
|
||||||
if not committed:
|
# commits from _commit_iteration, not just the final commit).
|
||||||
try:
|
has_branch_commits = False
|
||||||
# Check if branch has diverged from its base
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["git", "log", "--oneline", f"HEAD..{branch_name}"],
|
["git", "log", "--oneline", f"HEAD..{branch_name}"],
|
||||||
cwd=cwd, capture_output=True, text=True,
|
cwd=cwd, capture_output=True, text=True,
|
||||||
)
|
)
|
||||||
if not result.stdout.strip():
|
has_branch_commits = bool(result.stdout.strip())
|
||||||
# No commits on branch beyond base — clean up
|
except Exception:
|
||||||
subprocess.run(
|
pass
|
||||||
["git", "branch", "-D", branch_name],
|
|
||||||
cwd=cwd, capture_output=True,
|
|
||||||
)
|
|
||||||
logger.info(" Deleted empty branch: %s", branch_name)
|
|
||||||
except Exception:
|
|
||||||
pass # best-effort cleanup
|
|
||||||
|
|
||||||
return branch_name if committed else None
|
if has_branch_commits:
|
||||||
|
if not committed:
|
||||||
|
logger.info(" Agentic changes on branch: %s (from intermediate commits)", branch_name)
|
||||||
|
return branch_name
|
||||||
|
|
||||||
|
# No commits on branch at all — clean up
|
||||||
|
try:
|
||||||
|
subprocess.run(
|
||||||
|
["git", "branch", "-D", branch_name],
|
||||||
|
cwd=cwd, capture_output=True,
|
||||||
|
)
|
||||||
|
logger.info(" Deleted empty branch: %s", branch_name)
|
||||||
|
except Exception:
|
||||||
|
pass # best-effort cleanup
|
||||||
|
|
||||||
|
if not committed:
|
||||||
|
logger.warning(" No agentic changes to commit (empty diff)")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _run_simple_pipeline(
|
def _run_simple_pipeline(
|
||||||
|
|||||||
@@ -71,8 +71,11 @@ class BuiltinAgentConfigTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertIn("--dangerously-skip-permissions", coder_args)
|
self.assertIn("--dangerously-skip-permissions", coder_args)
|
||||||
self.assertIn("bypassPermissions", coder_args)
|
self.assertIn("bypassPermissions", coder_args)
|
||||||
self.assertIn("plan", reviewer_args)
|
# Reviewers/seniors use -p without --permission-mode plan
|
||||||
self.assertIn("plan", senior_args)
|
self.assertIn("-p", reviewer_args)
|
||||||
|
self.assertIn("-p", senior_args)
|
||||||
|
self.assertNotIn("plan", reviewer_args)
|
||||||
|
self.assertNotIn("plan", senior_args)
|
||||||
|
|
||||||
def test_codex_builtin_agents_skip_git_repo_check(self) -> None:
|
def test_codex_builtin_agents_skip_git_repo_check(self) -> None:
|
||||||
for agent_name in ("codex-coder", "codex-reviewer", "codex-senior"):
|
for agent_name in ("codex-coder", "codex-reviewer", "codex-senior"):
|
||||||
|
|||||||
Reference in New Issue
Block a user