Introduction
In modern software development, the primary bottleneck for both human developers and AI agents is often not the act of writing code, but the challenge of locating the relevant logic within a massive repository. In a codebase containing thousands of files, identifying the specific functions or classes that require modification—a task known as repository-level code retrieval—is a classic "needle in a haystack" problem.
Existing dense retrieval models like CodeBERT or CodeSage, while effective for general code search (e.g., finding a generic sorting algorithm), often fail in the context of code editing. These models typically suffer from semantic misalignment between bug reports and code logic, a loss of structural hierarchy, and a lack of awareness regarding functional dependencies. To address these gaps, we present CoRet, a specialized retriever designed to leverage the inherent structure and relationships within a software repository.
Methodology
CoRet introduces several architectural innovations to improve retrieval accuracy for code editing tasks. Rather than treating code as a flat sequence of text, CoRet incorporates the organizational and functional context of the repository.
Granular Chunking and Structural Context
Standard retrievers often process entire files, which can introduce significant noise and exceed context windows. CoRet instead breaks code down into "atoms"—individual functions, classes, or methods. To ensure these atoms do not lose their "forest for the trees" context, each chunk is prefixed with its full file path. This allows the model to learn that specific types of logic are often grouped within certain directory structures (e.g., /models/ vs. /utils/).
Call Graph Integration
Code functions rarely operate in isolation. To capture the functional dependencies of a codebase, CoRet integrates call graph information directly into the retrieval process. When a function (the caller) invokes another function (the callee), the callee's signature is appended to the caller's chunk using a special [DOWN] token. This provides the model with "downstream" semantic depth, helping it understand what a piece of code does by observing the tools it utilizes.
Optimized Training with In-Instance Negatives
While most retrievers are trained by comparing queries from one repository against code from another, CoRet utilizes "in-instance" negatives. By sampling irrelevant code from the same repository during training, the model is forced to learn the subtle, fine-grained differences between similar-looking functions within a single project. This is paired with a log-likelihood loss function that optimizes the probability of retrieving the correct code sections from within a specific repository context.
Experimental Results
CoRet was evaluated on two primary benchmarks: SWE-bench (Verified), which consists of real-world GitHub issues, and Long Code Arena (LCA), focused on long-context bug localization.
- Significant Recall Gains: On SWE-bench Verified, CoRet achieved a Recall@5 of 0.54, a 52.9% improvement over the base CodeSage S model (0.34).
- Consistency Across Baselines: CoRet consistently outperformed standard models such as BM25, CodeBERT, and UniXcoder by over 15 percentage points across both benchmarks.
- Perfect Recall: The model showed substantial improvements in "Perfect-Recall@k"—the ability to retrieve all necessary chunks for a multi-file edit—which is a critical requirement for downstream automated coding agents.
Conclusion and Future Work
CoRet demonstrates that incorporating repository-specific structural features—namely file hierarchies and call graphs—significantly enhances the performance of code retrievers. While currently optimized for Python, the underlying logic of granular chunking and dependency tracking is language-agnostic. Future research will focus on extending these techniques to multi-language support and exploring the integration of modern Large Language Models (LLMs) as encoders to further push the boundaries of automated code editing.
Summary updated