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

RLCoder: Reinforcement Learning for Repository-Level Code Completion

Yanlin Wang, Daya Guo, Jiachi Chen, Ruikai Zhang, Yuchi Ma, Zibin Zheng

Introduction

Modern Large Language Models (LLMs) have revolutionized code completion within individual files. However, they often struggle with "repository-level" completion tasks that require an understanding of variables, functions, or APIs defined across multiple files. To bridge this gap, developers use Retrieval-Augmented Generation (RAG) to pull relevant snippets from the broader codebase.

Despite its potential, RAG for code completion faces significant hurdles: a heavy reliance on expensive manually labeled data, the fragmentation of code context due to fixed-window chunking, and the "noise problem," where irrelevant retrieved snippets lead to model hallucinations. RLCoder addresses these challenges by introducing a reinforcement learning (RL) framework that trains a retriever without human labels, preserves semantic continuity, and selectively filters out unhelpful context.

The RLCoder Framework

RLCoder moves away from supervised learning by employing an RL-based approach to train its retriever, known as RLRetriever. The core innovation lies in using the LLM itself as an evaluator. During training, the retriever is rewarded if the context it provides makes the target code more "predictable" for the generator model.

The RLCoder architecture illustrating the training and inference pipelines. During training, the RLRetriever is optimized using a weighted perplexity reward from an evaluator.

Key Methodological Innovations

  1. Weighted Perplexity ($PPL_w$): To ensure the retriever focuses on the most critical parts of the code, RLCoder uses a weighted perplexity reward. Higher weights are assigned to the initial tokens of a completion and to specific identifiers like API names. This encourages the retriever to prioritize context that helps the model correctly identify external dependencies rather than just guessing common syntax.
  2. Split-Aggregate Strategy: Instead of arbitrary fixed-length windows, RLCoder splits files based on natural boundaries like blank lines. These "mini-blocks" are then aggregated until they reach a size limit, ensuring that functions and class definitions remain semantically intact.
  3. Stop Signal Mechanism: One of the most significant insights of the paper is that retrieval is not always beneficial. RLCoder introduces a "stop signal"—a null candidate in the search space. If the retriever determines that no external context is more relevant than the stop signal, it provides no context at all, preventing the generator from being misled by "distractor" snippets.

Addressing the Noise Problem

A common failure mode in RAG systems occurs when a retriever forces irrelevant information into the prompt. This can cause the generator to hallucinate attributes or methods that do not exist in the current context. By incorporating the stop signal, RLCoder can recognize when the information needed for completion is already present in the current file.

An example demonstrating how irrelevant retrieved context can lead to attribute errors, and how RLCoder's ability to skip retrieval (Generation without Retrieval) produces the correct code.

Experimental Results

The authors evaluated RLCoder on several benchmarks, including CrossCodeEval and RepoEval, as well as a custom GitHubEval dataset designed to test the stop signal mechanism.

  • State-of-the-Art Performance: RLCoder achieved a 12.2% improvement in Exact Match (EM) accuracy over the previous state-of-the-art method, RepoCoder, on the CrossCodeEval Python benchmark.
  • Model Agnostic: The framework demonstrated broad compatibility, enhancing the performance of various backbone models such as CodeLlama, StarCoder, and DeepSeekCoder.
  • Efficiency: Unlike iterative retrieval methods, RLCoder maintains high accuracy in a single retrieval-generation pass, making it more suitable for real-time developer tools.

Conclusion

RLCoder demonstrates that an LLM's internal probability (perplexity) is a powerful proxy for retrieval utility, eliminating the need for costly labeled datasets. By combining RL-driven training with a selective retrieval strategy and semantically aware code splitting, RLCoder significantly advances the reliability of repository-level code completion. Future work may involve expanding reward functions to include execution-based feedback, ensuring that completed code not only looks correct but also passes functional tests.

Summary updated

BibTeX
@inproceedings{wang2024rlcoder,
  author = {Yanlin Wang and Yanli Wang and Daya Guo and Jiachi Chen and Ruikai Zhang and Yuchi Ma and Zibin Zheng},
  title = {{RLCoder: Reinforcement Learning for Repository-Level Code Completion}},
  booktitle = {ICSE},
  year = {2024},
}
Figure