How Auto-Fix PR Generation Works End to End
From review findings to a fix pull request on your feature branch: finding filters, whole-file rewrites, the GitHub Git Trees API commit flow, quota handling, and the limits to review for.
When a CodePeel code review discovers security vulnerabilities or syntax bugs with concrete code patches, our automated remediation pipeline can package them into a clean pull request. Operating asynchronously in the background, it creates a dedicated fix branch on your repository without manual copy-pasting.
Key Takeaways
- Whole-File Rewrites Over Line Splices: To avoid brittle line offset collisions, the generator rewrites full file buffers with surrounding context.
- Git Trees API Assembly: Commits assemble directly via GitHub's Git Data API without local checkouts or heavy clone operations.
- Targeted Feature Branching: Fix pull requests target your working feature branch rather than
main, keeping review authority with you.- Non-Blocking Background Execution: The auto-fix pass runs in parallel with comment webhooks. If fix generation fails, standard review comments post without delay.
Auto-fix PR generation is an automated code remediation pipeline that turns static review findings into ready-to-merge GitHub pull requests. It bridges the gap between identifying code flaws and opening verified commits.
This walkthrough details the pipeline: finding filters, AST rewrite mechanics, GitHub Git Trees API commit assembly, and pull request safety boundaries.
Enabling Auto-Fix in your repository
Auto-fix is available on Pro and Max plans and is disabled by default:
CodePeel Web Dashboard
├── Global Toggle: Settings ──► Automation ──► Auto-Fix Findings
└── Repository Override: Repositories ──► [Select Repo] ──► Config ──► Auto-Fix
Repository settings take precedence over organization defaults. Once active, the engine evaluates every pull request review against its fixability criteria.
Step 1: the fixability filter
After a review pass completes, findings pass through an automated triage filter. To qualify for remediation, a finding must provide three properties:
- A structured code patch (
problemCodeandfixCodepair). - A verified line number in the modified file.
- A valid target file path matching the repository tree.
Incoming Review Findings
│
▼
Fixability Filter ──► Discards architectural advice & vague nits
│
▼
Group Findings by File Path
(Avoids conflicting duplicate rewrites)
│
▼
Full-Buffer AST Rewrite
Abstract observations (such as "decompose this class" or "add missing unit tests") require human domain context and are excluded. The pipeline focuses on high-precision, localized repairs: parameterizing raw SQL statements, replacing insecure eval() calls, inserting missing null coalescing operators, and correcting variable scoping.
Step 2: whole-file rewrites over line splicing

Applying line-based git patches naively causes frequent collisions. If multiple findings occur in one file, changing line lengths in the first patch breaks line coordinate anchors for subsequent patches.
To guarantee syntactical integrity, CodePeel uses full-buffer rewrites:
- The worker fetches the file content at the pull request's head commit via the GitHub Contents API.
- The full file text is transmitted to the fix generator alongside all applicable finding patches for that file.
- The model returns a single, coherent file rewrite with all fixes applied simultaneously.
Because the model inspects surrounding imports, variable names, and formatting, fixes blend naturally into existing code. The generated file is diffed against the original. If no changes occurred, the file is dropped. If all files remain identical, the pipeline exits cleanly without creating an empty pull request.
Step 3: commit assembly via GitHub Git Trees API
Fix commits are constructed directly in the cloud using the GitHub Git Data API. The server never clones repositories or checks out working directories:
1. Fetch Pull Request Head ──► Retrieve branch name and Head SHA
│
2. Query Tree SHA ──► GET /repos/.../git/commits/{head_sha}
│
3. Create New Tree Object ──► POST /repos/.../git/trees (base_tree = Head Tree)
│
4. Construct Signed Commit ──► POST /repos/.../git/commits (parent = Head SHA)
│
5. Create Fix Branch Ref ──► POST /repos/.../git/refs/heads/codepeel/fix-pr-...
│
6. Open Review Pull Request ──► POST /repos/.../pulls (base = Feature Branch)
By referencing the PR head commit as base_tree, only modified files are uploaded as git blobs. Unchanged files retain existing tree pointers. The newly generated commit lists your feature branch head as its direct parent, creating an unbroken commit history.
The commit metadata follows standard conventional commits:
fix(codepeel): apply 3 automated code review fixes
Co-authored-by: CodePeel <bot@codepeel.com>
The branch naming structure follows codepeel/fix-pr-<PR_NUMBER>-<TIMESTAMP>, preventing naming collisions on rapid commits.
The generated pull request experience
Fix pull requests target your active feature branch rather than main. This guarantees fixes undergo your normal automated CI suite, unit tests, and team review before entering production.
A typical automated patch resolves security issues cleanly:
--- a/src/db/users.ts
+++ b/src/db/users.ts
@@ -39,7 +39,7 @@
async findByEmail(email: string) {
- const result = await db.query(`SELECT * FROM users WHERE email = '${email}'`);
+ const result = await db.query('SELECT * FROM users WHERE email = $1', [email]);
return result.rows[0];
}
The PR description enumerates changed files and provides a verification checklist. A brief notification comment links the fix PR on your original pull request thread.
Quotas and failure isolation
Generating a fix PR consumes a single review allowance from your plan, deducted only after a pull request successfully publishes. If a review yields zero fixable issues or network timeouts occur, no allowances are deducted.
Because the auto-fix pipeline runs asynchronously alongside inline comment webhooks, slow remediation passes never delay code review comments on GitHub.
Architectural limits
- Multi-file coordinated refactors: Files rewrite independently. Cross-file refactors requiring synchronized signature changes are excluded.
- Deleted or renamed paths: If a file was deleted or renamed in a recent commit, patches for that path fail silently.
- Human approval required: Automated fix PRs never merge themselves. They require standard peer approval and green pre-merge quality gates.
To evaluate how effectively automated fixes improve repository defect density, review our testing benchmark in How to Benchmark an AI Code Reviewer.
