Debugging Actions & Autofix Failures
Relevant source files
The following files were used as context for generating this wiki page:
The autofix.py script identifies and repairs issues within pull requests by integrating AI-driven analysis with local validation. It operates in two primary modes: fixing findings from the PR-Agent code reviewer and repairing Continuous Integration (CI) build or test failures.
When an error occurs during the automated repair process, the system rolls back changes to maintain the integrity of the codebase. You can monitor these failures through GitHub PR comments, which include error snippets and validation status.
Sources: scripts/autofix.py:5-20, README.md:1-10
Autofix Operation Modes
The system selects its behavior based on the presence of code review findings or active test failures.
Review-Fix Mode
The script queries the GitHub API for review comments and summaries posted by PR-Agent. It filters for actionable bugs and sends the flagged code context to the Gemini AI model for remediation.
CI-Fix Mode
If no review findings exist, the script runs a detected test command. On failure, it extracts file paths from the error logs and asks the AI to diagnose the root cause and provide a fix.
The diagram shows the decision logic used to determine if the script should fix review comments or repair CI failures.
Sources: scripts/autofix.py:322-355
Failure Detection and Diagnostics
The system identifies failures by monitoring shell exit codes and parsing console output for file references.
Test Runner Detection
The script scans the repository structure to identify the appropriate test command. It prioritizes specific directories like v2/ or ui/mobile/ before checking root-level configuration files.
| Configuration File | Primary Command | Language/Framework |
|---|---|---|
Cargo.toml | cargo check --workspace | Rust |
package.json | npm test or npm run build | Node.js |
pyproject.toml | pytest -q | Python |
go.mod | go test ./... | Go |
Sources: scripts/autofix.py:65-90
Error Parsing
When a test fails, the extract_relevant_files_from_error function uses regular expressions to find file paths and line numbers within the error log. This ensures the AI receives the specific context needed for the repair.
Sources: scripts/autofix.py:265-275
Verification and Rollback Logic
Every applied fix undergoes a re-verification step. If the local test command fails after applying an AI-generated patch, the script rolls back the changes.
The sequence diagram illustrates the lifecycle of a repair attempt, highlighting the mandatory validation before code is committed.
Sources: scripts/autofix.py:202-235
Environment Configuration
The script requires specific environment variables to interact with the Gemini API and the GitHub repository.
| Variable | Requirement | Description |
|---|---|---|
GEMINI_API_KEY | Required | API key for Google AI Studio. |
GH_TOKEN | Required | GitHub token for API requests and comments. |
REPO | Required | Format: owner/repo. Used for fetching comments. |
PR_NUMBER | Required | The pull request ID for status updates. |
HEAD_REF | Optional | The branch name for pushing fixes. |
Sources: scripts/autofix.py:23-33, scripts/autofix.py:324-335
Troubleshooting Common Failures
- Insecure Path Rejection: The
safe_normpathfunction rejects any file paths that attempt to escape the repository root or use absolute paths. - Gemini API Errors: Failures during AI invocation (e.g., HTTP 4xx or 5xx) result in a fallback to CI-fix mode or a graceful exit with a PR comment.
- Empty Repair Result: If the AI determines no changes are necessary or cannot generate a valid patch, the script logs the determination and takes no action.
Sources: scripts/autofix.py:157-162, scripts/autofix.py:126-140, scripts/autofix.py:302-310
The autofix system ensures code quality by automating the feedback loop between review findings and verified code commits. By prioritizing local validation, it prevents the introduction of broken code into the pull request.
Sources: scripts/autofix.py:239-250, README.md:12-25