Back to Community & Earn Hub

Agentic CI/CD Self-Healing Pipelines: Using Ephemeral Git Worktrees and Automated AST Bisection to Auto-Fix Failing PRs

@rohan_mehta5 hours ago·2 min read 1903 views
Agentic CI/CD Self-Healing Pipelines: Using Ephemeral Git Worktrees and Automated AST Bisection to Auto-Fix Failing PRs

The CI Triage Bottleneck

In high-velocity monorepos, over 35% of CI failures are trivial, deterministic breakages:

  • A shared protobuf or OpenAPI schema changed on main, breaking a downstream TypeScript client import.
  • A linter or strict mypy/tsc rule flagged an unhandled None return.
  • A unit test assertion broke after an intentional copy or status-code update.

Yet these minor failures block PR queues for hours while engineers context-switch back to read CI logs.


Architecture: Ephemeral git worktree Self-Healing Loop

Rather than mutating shared checkouts or cloning multi-gigabyte repositories from scratch on every failure, modern CI agents spawn ephemeral git worktree sandboxes backed by a single local bare object store:

text
[GitHub Actions Webhook: check_run.completed (failure)]
         │
         ▼
[Extract Compact Failure Context]
  • Failing test name + truncated stack trace (last 60 lines)
  • Changed files diff (`git diff origin/main...HEAD`)
         │
         ▼
[Create Isolated Sandbox: `git worktree add /tmp/fix-<run_id> -b autofix/<pr>`]
         │
         ▼
[Bounded Coding Agent (Max 4 Iterations)]
  1. Inspect failing file & AST callers
  2. Apply surgical patch
  3. Re-run ONLY failing test target (`pytest tests/test_auth.py`)
         │
         ├──► Pass: Push `autofix/<pr>` & post review suggestion!
         └──► Fail after 4 tries: Cleanup worktree & escalate to human

Python Worktree Runner & Context Extractor

Here is the clean lifecycle wrapper ensuring every failed CI attempt leaves zero dirty state on the runner:

python
import subprocess
import tempfile
from pathlib import Path

class EphemeralWorktreeSandbox:
    def __init__(self, base_repo: str, commit_sha: str, branch_name: str):
        self.base_repo = Path(base_repo)
        self.commit_sha = commit_sha
        self.branch_name = branch_name
        self.worktree_dir = Path(tempfile.mkdtemp(prefix="ci-heal-"))

    def __enter__(self) -> Path:
        subprocess.run(
            ["git", "worktree", "add", "-b", self.branch_name, str(self.worktree_dir), self.commit_sha],
            cwd=self.base_repo,
            check=True,
            capture_output=True,
        )
        return self.worktree_dir

    def __exit__(self, exc_type, exc_val, exc_tb):
        # Always prune worktree even if agent crashes
        subprocess.run(
            ["git", "worktree", "remove", "--force", str(self.worktree_dir)],
            cwd=self.base_repo,
            capture_output=True,
        )
        subprocess.run(
            ["git", "worktree", "prune"],
            cwd=self.base_repo,
            capture_output=True,
        )

Guardrails That Keep Auto-Fixes Safe

  1. Never auto-fix security or auth tests: Skip any PR touching auth/, crypto/, or migrations/ via path globs.
  2. Enforce diff-size caps: Reject any agent-generated patch exceeding 150 changed lines or modifying tests just to make them pass (tests/ modifications require explicit human sign-off).
  3. Zero-copy speed: Creating a git worktree from a warm local mirror takes <120ms, compared to 45 seconds for a full git clone.
2 Comments

Discussion

@aarav_sharma5 hours ago

Sub-120ms git worktree sandboxes from a shared bare mirror are so much faster than spinning up fresh git clone --depth 1 containers on every CI webhook. And blocking tests/ edits by default stops agents from 'fixing' failures by deleting assertions.

@aditya_verma5 hours ago

Capping context to the last 60 lines of traceback + the PR diff keeps the self-healing agent focused. Dumping a 10,000-line raw GitHub Actions log almost always sends the model down a rabbit hole.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.