Autofix Script Automation
Relevant source files
The following files were used as context for generating this wiki page:
The Autofix Script Automation serves as an autonomous repair agent for pull requests. It identifies code review findings flagged by the PR-Agent and repairs CI build or test failures. The system operates by sending file contexts and error logs to the Gemini AI model to generate surgical code fixes, which it then verifies and commits back to the repository.
This automation operates within the broader context of the PR-Agent Central Configuration managed by the @JWEB0689 organization. It complements the standard PR-Agent tools like /review and /improve by providing an automated loop for applying suggested changes and fixing regressions.
Sources: scripts/autofix.py:1-20, README.md:1-10
Operation Modes
The autofix.py script executes in two distinct modes depending on the current state of the Pull Request.
Review-Fix Mode
If the script detects existing PR-Agent findings (inline comments or review summaries), it enters review-fix mode. It collects the flagged code snippets, queries Gemini for corrections, and applies minimal changes to resolve the issues.
Sources: scripts/autofix.py:279-282, scripts/autofix.py:317-320
CI-Fix Mode
If no review findings are present, the script executes the repository's native test suite. If tests fail, it extracts error snippets and file paths from the log to generate a repair patch via Gemini.
Sources: scripts/autofix.py:339-354
The following flowchart illustrates the decision logic for selecting the operation mode:
The diagram shows the fallback mechanism where the script attempts to fix review comments first before falling back to CI failure repairs.
Sources: scripts/autofix.py:378-398
Core Architecture and Data Flow
The automation utilizes a centralized script that interacts with the GitHub REST API and the Gemini Generative Language API.
Environment Configuration
The script requires specific environment variables for authentication and target identification.
| Variable | Requirement | Description |
|---|---|---|
GEMINI_API_KEY | Required | Google AI Studio key for Gemini model access. |
GEMINI_MODEL | Optional | Defaults to gemini-3.8-flash. |
GH_TOKEN | Required | GitHub token for querying comments and pushing commits. |
REPO | Required | The owner/repo string for the target repository. |
PR_NUMBER | Required | The pull request ID for status commenting. |
HEAD_REF | Required | The PR head branch for pushing repairs. |
Sources: scripts/autofix.py:22-35
AI Prompting and Result Parsing
The script uses structured JSON prompts to ensure Gemini returns machine-readable patches. It employs specific system prompts for review fixes (REVIEW_SYSTEM_PROMPT) and CI repairs (CI_SYSTEM_PROMPT).
# scripts/autofix.py:186-200
REVIEW_SYSTEM_PROMPT = """You are an autonomous senior software engineer fixing code review findings.
...
Respond ONLY with a JSON object conforming strictly to this schema:
{
"summary": "Short explanation of what was fixed",
"files": [
{"path": "relative/path/to/file", "content": "Full, complete updated file contents"}
]
}
"""
Sources: scripts/autofix.py:186-200, scripts/autofix.py:202-214
Verification and Safety Mechanisms
To prevent breaking code, the script includes several safety guards:
- Test Runner Detection: The
detect_test_runnerfunction identifies the project type (Rust, Node.js, Python, Go) and selects the appropriate validation command (e.g.,cargo check,npm test,pytest). - Path Normalization: The
safe_normpathfunction rejects any file paths that attempt to escape the repository root. - Local Re-verification: Before pushing any change, the script runs the detected test command. If the repair fails the test, the script rolls back changes using
git checkout -- .. - Size Limits: To manage API costs and context limits, files exceeding
MAX_FILE_BYTES(80,000 bytes) are ignored.
Sources: scripts/autofix.py:65-90, scripts/autofix.py:165-170, scripts/autofix.py:246-258
Verification Sequence
The sequence diagram details the verification loop that ensures only passing code is committed to the PR branch.
Sources: scripts/autofix.py:245-276
Integration with JWEB0689 Fleet
The script is intended to be used within the JWEB0689/pr-agent-settings ecosystem. The README.md indicates that settings in .pr_agent.toml are inherited globally across all repositories in the organization. This allows the Autofix Script Automation to maintain a consistent configuration for PR-Agent behaviors, such as determining which files to ignore or how to format descriptions.
Sources: README.md:1-25, .pr_agent.toml
The repository also provides a reusable GitHub Actions workflow (.github/workflows/pr_agent.yml) that repositories can call to trigger these autonomous repairs.
Sources: README.md:20-23, .github/workflows/pr_agent.yml
Conclusion
Autofix Script Automation provides a robust mechanism for closing the loop between AI code review and code implementation. By combining Gemini's reasoning capabilities with localized test verification, the system reduces the manual effort required to address common review findings and CI failures. All repairs are non-destructive, as the system never merges code; it only pushes suggested fixes to the head branch for human review.
Sources: scripts/autofix.py:17-18, scripts/autofix.py:270-276