AI Context Management
Relevant source files
The following files were used as context for generating this wiki page:
AI Context Management identifies, retrieves, and organizes repository data to provide the Gemini generative model with the necessary information to perform autonomous code repairs. This system ensures that the AI receives relevant file contents, review findings, and build error logs while staying within defined token and size constraints.
The management process occurs primarily within the autofix.py script, which operates in two distinct modes: review-fix and ci-fix. Each mode employs specific logic to gather context from GitHub APIs and the local filesystem to construct a comprehensive prompt for the AI model.
Sources: scripts/autofix.py:1-25, README.md:1-10
Context Retrieval Logic
The system retrieves context based on the current operational mode and the state of the Pull Request (PR).
File Context Selection
The read_file_context function maps file paths to their full string contents. To maintain performance and model reliability, the system enforces hard limits on the amount of data sent to the AI.
| Constraint | Value | Description |
|---|---|---|
MAX_FILE_BYTES | 80,000 | Maximum size allowed for a single file to be included in context. |
MAX_CONTEXT_FILES | 12 | Maximum number of files included in a single AI request. |
Sources: scripts/autofix.py:33-34, scripts/autofix.py:84-98
Mode-Specific Context
The logic for selecting which files enter the context varies by the detected failure type:
- Review Findings: In
review-fixmode, the system retrieves inline comments and summary reports. It adds any file explicitly mentioned in a review comment to the context. - Build/Test Failures: In
ci-fixmode, the system parses the execution logs of the detected test runner (e.g.,pytest,npm test,go test). It extracts file paths associated with compiler or runtime errors.
Sources: scripts/autofix.py:204-230, scripts/autofix.py:302-315
Data Flow Architecture
The following diagram illustrates how the system aggregates data from external APIs and the local environment to build the AI context.
The diagram shows the aggregation of GitHub API data and local repository state into the context collector before prompting the AI.
Sources: scripts/autofix.py:114-175, scripts/autofix.py:270-290
Review Finding Extraction
The system gathers context from human or AI reviewers via the GitHub REST API. This process targets two types of feedback:
- Inline Comments: Specific lines of code flagged in the PR.
- Summaries: Broad feedback typically provided by the PR-Agent bot as an issue comment.
The get_review_findings function filters out utility comments (like /autofix triggers) and limits the length of body text to ensure the context remains concise.
Sources: scripts/autofix.py:182-212
Error Log Parsing
When no review findings exist, the system enters ci-fix mode. It executes a test runner and captures the output. The extract_relevant_files_from_error function uses regular expressions to scan the output for file paths and line numbers.
def extract_relevant_files_from_error(error_log):
"""File paths mentioned in compiler/runtime errors."""
extracted = set()
# Matches Rust-style arrows --> path/to/file.rs:line
for m in re.finditer(r"-->\s+([a-zA-Z0-9_\-/\.]+\.\w+):(\d+)", error_log):
if os.path.isfile(m.group(1).strip()):
extracted.add(m.group(1).strip())
# Matches standard path:line patterns for common languages
for m in re.finditer(r"([a-zA-Z0-9_\-/\.]+\.(?:rs|ts|tsx|js|jsx|py|go|toml|json)):(\d+)", error_log):
if os.path.isfile(m.group(1).strip()):
extracted.add(m.group(1).strip())
return list(extracted)
Sources: scripts/autofix.py:302-315
Global vs. Local Configuration
The AI's behavior and the tools it applies are governed by a hierarchical configuration system managed by PR-Agent.
- Global Settings: Defined in the
JWEB0689/pr-agent-settingsrepository within.pr_agent.toml. This applies to all repositories in the organization. - Repository Overrides: Individual repositories can define their own
.pr_agent.tomlto override specific context or tool behaviors.
Sources: README.md:12-23
Summary of Context Elements
| Element | Source | Usage |
|---|---|---|
findings | GitHub Pull Request API | Provides the "Instructions" for what needs to be fixed. |
modified_files | git diff | Identifies the scope of the current change. |
contexts | Local Filesystem | Provides the "Source Material" for the AI to rewrite. |
test_cmd | detect_test_runner | Validates if the context-driven fix actually works. |
Sources: scripts/autofix.py:48-81, scripts/autofix.py:221-226, scripts/autofix.py:328-333