Abstract
The escalating complexity of modern codebases has intensified the need for code retrieval systems capable of interpreting cross-component change intents—a capability fundamentally absent in conventional function-level search paradigms. While recent research has improved alignment between queries and code snippets, retrieving contextually relevant code for certain change request remains underexplored. To bridge this gap, we present RepoAlignBench, the first benchmark designed to evaluate repository-level code retrieval for change request-driven scenarios, encompassing 52k samples. The benchmark shifts the paradigm from function-centric retrieval to holistic repository analysis. In addition, we propose ReflectCode, an adversarial reflection-augmented dual-tower architecture featuring disentangled code_encoder and doc_encoder towers. Our framework dynamically integrates syntactic patterns, function dependency, and semantic expansion intent through LLM. Comprehensive evaluations demonstrate that ReflectCode achieves 12.2% Top-5 Accuracy and 7.1% Recall improvements over state-of-the-art baselines.
Introduction
Modern software development involves navigating vast, interconnected codebases where a single change request often spans multiple files and modules. Traditional code retrieval systems, however, remain largely "function-centric," excelling at finding isolated snippets but failing to capture the "cross-component" intents inherent in real-world engineering tasks. There is a significant semantic gap between a developer's high-level intent (e.g., "Fix the data integrity issue in the concurrency layer") and the distributed code segments required to address it. Existing benchmarks have historically lacked the complexity to test a model's ability to navigate these repository-wide relationships.
ReflectCode Architecture
To address these limitations, we introduce ReflectCode, a novel architecture that employs a dual-tower approach combined with an adversarial verification loop.
The architecture consists of two primary components:
- The Generator: Utilizes a dual-tower setup with C-BERT (Code Encoder) for processing structural-aware code syntax and N-BERT (Text Encoder) for natural language queries. These towers are disentangled to prevent "feature entanglement," ensuring that programming logic and natural language nuances are captured independently.
- The Discriminator (Adversarial Search Mechanism): Instead of relying solely on vector similarity, ReflectCode incorporates a discriminator powered by a Large Language Model (e.g., Llama 3.1). The discriminator performs multi-hop reasoning to verify the relevance of retrieved candidates. If a mismatch is detected, it provides "reflective text" feedback to iteratively refine the search.
Training is further enhanced by an adaptive-margin triplet loss, which dynamically adjusts penalties based on the difficulty of negative examples, forcing the model to learn fine-grained distinctions between similar-looking but irrelevant code.
RepoAlign-Bench: A Repository-Level Benchmark
Accompanying the model is RepoAlign-Bench, the first benchmark specifically designed for repository-level, change-oriented retrieval.
RepoAlign-Bench comprises 52,000 samples derived from real-world GitHub pull requests. It utilizes Tree-sitter for robust Abstract Syntax Tree (AST) parsing, ensuring structural fidelity. The dataset is stratified into three difficulty tiers:
- Full: Focuses on basic pattern recognition.
- Challenge: Requires contextual disambiguation.
- Expert: Demands complex, cross-functional reasoning across multiple modules (e.g., ensuring atomic writes across different layers).
Key Results
ReflectCode demonstrates substantial performance gains over established baselines such as CodeBERT, GraphCodeBERT, and InCoder:
- Accuracy: Achieved a 59.64% Top-5 Accuracy on the full dataset, more than doubling the performance of CodeBERT (28.93%).
- Expert Tasks: Maintained a 34.86% Top-5 accuracy on the most difficult tasks, where other models struggled to exceed 20%.
- Recall: Improved recall by 7.1%, indicating a superior ability to find diverse, relevant code segments that span different parts of a repository.
- Efficiency: The use of Llama 3.1-8B as a discriminator provides a balance between high-quality verification and manageable latency (~149ms).
Conclusion
By shifting the focus from isolated functions to holistic repository analysis, ReflectCode and RepoAlign-Bench provide a new foundation for intelligent code retrieval. While the LLM-powered verification loop introduces some latency, it significantly improves the precision of retrieved results for complex change requests. Future work will focus on reducing latency for real-time IDE integration, expanding support to low-resource languages like Rust, and incorporating advanced program analysis to capture implicit runtime dependencies.
Summary updated