NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

AlignCoder: Aligning Retrieval with Target Intent for Repository-Level Code Completion

Tianyue Jiang, Yanli Wang, Daya Guo, Ensheng Shi, Yuchi Ma, Jiachi Chen, Zibin ZhengarXiv 2026

TL;DR

AlignCoder is a novel framework that significantly boosts repository-level code completion by bridging the semantic gap between unfinished code queries and target code snippets. By generating multiple candidate completions in parallel to enhance the retrieval query and training the retriever via reinforcement learning, it achieves up to an 18.1% absolute improvement in Exact Match scores over existing state-of-the-art methods.

What Problem Does This Paper Address?

While Large Language Models (LLMs) excel at simple, single-file code completion, repository-level code completion remains a major hurdle. Modern software projects rely heavily on complex cross-file dependencies, custom APIs, and proprietary class hierarchies that LLMs haven't seen during pre-training.

To fix this, developers use Retrieval-Augmented Generation (RAG) to pull relevant code snippets from the repository to serve as context for the LLM. However, current RAG methods suffer from two core flaws:

  1. Semantic Misalignment: Unfinished code is often a poor search query because it lacks the very tokens (like specific method names or API calls) present in the target code. While some methods try to fix this by iteratively generating code and re-retrieving, a single mistake in an early iteration can cause cascading "chain errors."
  2. Untrained Retrievers: Standard dense or sparse retrievers aren't trained to understand the relationship between unfinished code and generated intermediate completions, failing to fully exploit this "inference information."
Fig. 1. Motivating example. Limitations of prior works: ReACC and RLCoder use the unfinished code as the query, which may miss key tokens in the ground truth. RepoCoder may cause chain errors and has potential efficiency issue.

Methods

AlignCoder introduces a three-stage pipeline designed to solve these alignment and training gaps:

  1. Codebase Construction: Cross-file context is broken down using a "Split-Aggregate" strategy to maintain code continuity. Additionally, it uses tree-sitter to extract explicit dependency code snippets (like class method signatures and API interactions) to provide deep semantic context.
  2. Query Enhancement Mechanism: Instead of running slow, error-prone iterative retrieval loops, AlignCoder uses a lightweight LLM sampler to generate multiple candidate completions in parallel based on a quick initial search. These candidates are appended to the unfinished code to create an Enhanced Query. This drastically increases the odds of capturing the correct key tokens early on.
  3. RL-based Retriever Alignment (AlignRetriever): The retriever is fine-tuned using Reinforcement Learning. An evaluator LLM calculates the Perplexity (PPL) of the ground-truth target code when using the retrieved snippets as context. If a snippet helps the model predict the target code with higher confidence (lower perplexity), the retriever receives a positive reward, aligning its search parameters directly with the generator's needs.
Fig. 3. Overview of AlignCoder. Our approach utilizes the inference capability of the LLM to sample multiple candidate completions and learn to understand the inference information for accurate retrieval for repository-level code completion.

Datasets

  • Training: 10,000 Python and Java repositories scraped from GitHub, filtered to ensure cross-file dependencies and topologically sorted to mimic realistic development workflows.
  • Evaluation:
  • CrossCodeEval: A rigorous, multilingual benchmark explicitly designed for cross-file code completion context (tested on Python and Java).
  • RepoEval: A widely used repository-level benchmark focusing on line-level and API invocation tasks.

Key Insights and Ideas

  • The Sweet Spot of Multiple Sampling: Because LLMs are inherently stochastic, relying on a single generated completion for retrieval is risky. Sampling multiple candidates in parallel acts as a safety net. However, the authors mathematically demonstrate a law of diminishing returns: too many samples introduce noise that clutters the query. They find that 4 samples strikes the perfect balance between accuracy and computational efficiency.
  • Perplexity as an Unsupervised Reward: Instead of relying on brittle lexical matching (like BM25) or human labels, using an LLM's internal perplexity score as a reward signal allows the retriever to adapt dynamically to what actually helps the code generator reason better.
Fig. 4. Example: the initial query and the enhanced query obtained with the query enhancement mechanism. Task id: project_cc_python/74.

Key Results

AlignCoder was evaluated across five backbone code LLMs (including CodeLlama-7B, StarCoder-7B, and DeepSeekCoder-7B) and consistently outperformed all baselines:

  • CrossCodeEval (Python): Achieved a massive 18.1% absolute improvement in Exact Match (EM) score when using DeepSeekCoder-1B compared to the previous best baseline (RLCoder).
  • CrossCodeEval (Java): Showed up to a 14.0% EM improvement using StarCoder2-7B.
  • Ablation Studies: Removing the Reinforcement Learning component (w/o RL) caused the most severe performance drops across all datasets, validating that retrievers must be explicitly trained to interpret enhanced queries.

Limitations and Future Work

While highly effective, AlignCoder introduces some computational overhead during the query enhancement phase due to generating multiple candidate completions (though this is mitigated by using a lightweight sampler and the optimized vLLM inference framework). Furthermore, if the initial sampler is exceptionally weak, the quality of the enhanced query degrades.

The authors suggest that future work could explore tighter, end-to-end integration between the sampler and the retriever, or optimize the candidate selection mechanism to filter out noisy completions before they enter the query.

Summary updated

BibTeX
@misc{jiang2026aligncoder,
  author = {Tianyue Jiang and Yanli Wang and Yanlin Wang and Daya Guo and Ensheng Shi and Yuchi Ma and Jiachi Chen and Zibin Zheng},
  title = {{AlignCoder: Aligning Retrieval with Target Intent for Repository-Level Code Completion}},
  year = {2026},
  eprint = {2601.19697},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2601.19697},
}
Figure