Writing Custom Autofix Modules
Relevant source files
The following files were used as context for generating this wiki page:
The autofix system provides autonomous repair capabilities for pull requests by addressing findings from AI code reviews or repairing CI build and test failures. It operates as a standalone Python script that interfaces with the Google Gemini API to generate minimal, surgical code changes based on context extracted from the repository.
The system functions in two primary modes: review-fix, which targets specific inline comments and summaries, and ci-fix, which reacts to execution errors detected by local test runners. Every change undergoes local verification before the script commits and pushes the fix to the PR branch.
Sources: scripts/autofix.py:1-26, README.md:1-20
Core Architecture and Execution Flow
The autofix module follows a linear execution path that prioritizes explicit review findings before falling back to general CI failure repairs. It utilizes environment variables for configuration and relies on the GitHub CLI (gh) and Git for repository interactions.
Logic Flow Diagram
The following diagram illustrates the decision logic used to select the repair mode and the subsequent validation loop.
The workflow prioritizes review_fix_mode if review comments exist, then executes ci_fix_mode if validation commands fail. Sources: scripts/autofix.py:321-346
Operational Modes
Review-Fix Mode
The review_fix_mode function identifies issues flagged by PR-Agent or other reviewers. It collects inline comments and review summaries using the GitHub REST API and sends this text along with file contents to the LLM.
- Targeting: It focuses on files specifically mentioned in findings or modified in the current branch.
- Filtering: The system prompt instructs the AI to skip style nitpicks and focus on bugs or security issues.
Sources: scripts/autofix.py:157-183, scripts/autofix.py:228-264
CI-Fix Mode
The ci_fix_mode function triggers when a repository's standard check command (e.g., npm test, cargo check) fails. It extracts file paths from error logs to narrow the context provided to the LLM.
- Error Extraction:
extract_relevant_files_from_erroruses regular expressions to find paths and line numbers in compiler or runtime output. - Context Generation: It includes modified files and files mentioned in the error logs.
Sources: scripts/autofix.py:277-318, scripts/autofix.py:267-275
LLM Integration and Prompting
The system communicates with Gemini using specific system prompts that enforce a strict JSON response schema. This structure ensures the script can programmatically apply file updates.
JSON Response Schema
All repairs must return the following structure:
{
"summary": "Short explanation of the fix",
"files": [
{
"path": "relative/path/to/file",
"content": "Full, complete updated file contents"
}
]
}
Sources: scripts/autofix.py:185-207
Configuration Parameters
| Variable | Description | Default |
|---|---|---|
GEMINI_API_KEY | Required Google AI Studio key | N/A |
GEMINI_MODEL | The specific model to use for generation | gemini-3.8-flash |
REPO | GitHub repository in owner/repo format | N/A |
PR_NUMBER | The ID of the current Pull Request | N/A |
BASE_REF | Base git reference for diffing | origin/main |
Sources: scripts/autofix.py:28-36, scripts/autofix.py:322-331
Automated Test Detection
The script automatically determines how to validate changes based on the presence of specific configuration files in the repository.
The function detect_test_runner returns the working directory and the command string. Sources: scripts/autofix.py:65-92
Security and Validation Mechanisms
To prevent destructive changes or directory traversal attacks, the module implements several safeguards:
- Path Normalization:
safe_normpathensures file paths do not escape the repository root or use absolute paths. - Size Limits: Files exceeding
MAX_FILE_BYTES(80,000 bytes) are excluded from the LLM context to prevent token overflow. - Verification Loop: The
verify_commit_pushfunction runs the detected test command after applying changes. If the command fails, the script executesgit checkout -- .to roll back changes. - Ownership Verification: Changes are only pushed to the
HEAD_REFbranch, and specific Git configurations are applied for thegithub-actions[bot].
Sources: scripts/autofix.py:38-39, scripts/autofix.py:150-155, scripts/autofix.py:228-235, scripts/autofix.py:241-260
Conclusion
The custom autofix module integrates LLM-driven code generation with standard Git workflows and language-specific test runners. By targeting both explicit review feedback and implicit CI failures, it provides a comprehensive autonomous repair loop that maintains repository integrity through strict post-repair validation.