Abstract
This paper presents a multi-stage reranking system for repository-level code search, which leverages the vastly available commit histories of large open-source repositories to aid in bug fixing. We define the task of repository-level code search as retrieving the set of files from the current state of a code repository that are most relevant to addressing a user's question or bug. The proposed approach combines BM25-based retrieval over commit messages with neural reranking using CodeBERT to identify the most pertinent files. By learning patterns from diverse repositories and their commit histories, the system can surface relevant files for the task at hand. The system leverages both commit messages and source code for relevance matching, and is evaluated in both normal and oracle settings. Experiments on a new dataset created from 7 popular open-source repositories demonstrate substantial improvements of up to 80% in MAP, MRR and P@1 over the BM25 baseline, across a diverse set of queries, demonstrating the effectiveness this approach. We hope this work aids LLM agents as a tool for better code search and understanding. Our code and results obtained are publicly available.
Introduction
While Large Language Models (LLMs) like GPT-4 are revolutionizing coding assistance, they still struggle with "repository-level" tasks—complex bugs that require understanding interactions across many files. Simply stuffing an entire codebase into an LLM's context window is expensive, slow, and often leads to "hallucinations" or degraded performance. The challenge is retrieval: finding the 5–10 most relevant files in a repository of 10,000+ files to provide the LLM with the necessary context. Most existing tools focus on finding small snippets, but real-world bug fixing requires identifying the right files. This research leverages the rich history of past commits as a roadmap for solving new issues.
Methodology
The proposed system employs a three-stage pipeline to narrow the search space from thousands of files to a handful of highly relevant ones.
- BM25 Initial Ranker: A classic keyword-based search that identifies previous commits with messages similar to the user's query. It aggregates scores to identify files modified in those past commits, narrowing the field from ~10,000 to ~1,000 files.
- CommitReranker (Neural): Utilizing CodeBERT, this stage performs a semantic match between the user's query and past commit messages. This overcomes "vocabulary mismatch" (e.g., matching "crash" with "segmentation fault"), further narrowing the list to a few hundred files.
- CodeReranker (Neural): The most granular stage, which uses CodeBERT to examine the actual source code (patches/diffs) of candidate files. To handle long files, a line-wise sliding window splits code into patches to find the most relevant sections.
The system is trained using supervised contrastive finetuning, distinguishing between files that solved a problem and those that did not.
Dataset and Evaluation
The researchers created a large-scale dataset from seven popular open-source repositories, totaling approximately 150 GB of data. This dataset includes full commit histories and file versions, providing a rich source for training neural models on real-world software evolution.
To simulate real-world queries, an evaluation set (GPT-M) was created using GPT-4 to transform past commit messages into "issue descriptions," with solutions masked to prevent data leakage.
Results and Insights
The multi-stage pipeline demonstrated massive gains over the BM25 baseline, achieving up to an 80% improvement in Mean Average Precision (MAP) and Mean Reciprocal Rank (MRR). In a non-oracle setting, the system achieved a P@1 of 0.3 and a Recall@10 of 0.47.
Key insights include:
- Commit History as a Bridge: Past commits serve as a proxy for future bugs; similar issues often involve the same files.
- Cross-Repository Generalization: Training on a combined dataset of multiple repositories yielded better results, suggesting the model learns general patterns of bug-related code that transfer across projects.
- Optimal Reranking Depth: Performance peaked when reranking the top 100 results; deeper reranking (e.g., top 1,000) introduced noise and decreased precision.
Conclusion
This approach demonstrates that providing concise, highly relevant context is far more effective for LLMs than massive, noisy contexts. By leveraging historical commit data, the system effectively bridges the gap between natural language queries and relevant source code files. Future work will focus on reducing the computational cost of the CodeReranker and exploring dense embeddings for the initial retrieval step.
Summary updated