Manuscript Offprint

arXiv:2611.07510

Enhancing Repository-Level Code Completion with Reinforcement Learning in Real-World Scenarios

Zhenlong Dai, Zhuoluo Zhao, Xiu Tang, Sai Wu, Chang Yao, Zhipeng Gao, Jingyuan Chen

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

The rapid development of large language models (LLMs) for code, which closely aligns with the practical needs of real-world software development, has sparked growing attention in repository-level code completion. However, existing Code LLMs present challenges in focusing on suitable contexts from the repository and in deeply reasoning about cross-file dependencies. To address these challenges, we propose a novel reinforcement learning framework for repository-level code completion. To better understand both in-file and cross-file contexts, we employ identifier-driven intent recognition to capture completion intent, thereby improving the model's performance in real-world scenarios. To enhance cross-file reasoning, we propose a reward-driven completion learning to effectively reward in complex repository completion scenarios. To guide LLMs in completing code that aligns with intended functionality and repository context, we introduce a selective-exploration strategy that directs the model to focus on low-confidence, high-reward tokens, promoting the exploration of valuable, underexplored completion patterns. Experimental results show that our approach significantly improves the performance of Code LLMs. The code and the dataset are available at https://github.com/zhenlongDai/RepoCompletion.

1 Introduction

With the rapid development of large language models (LLMs) for code (Jiang et al., 2024a; Zheng et al., 2023b), these LLMs have demonstrated advanced capabilities in code understanding and completion. Many LLMs are integrated into IDEs to support developers' daily development, one of the most common usage scenarios is code completion (Zhou et al., 2022; Wang et al., 2023a). Previous studies have explored code completion tasks

Figure

Figure 1: Situations of repo-level code completion.

on different granularities, such as line-level code completion (Liu et al., 2024; Izadi et al., 2022), function-level code completion (Li et al., 2022; Quan et al., 2025). More recently, the repository-level code completion (Zhang et al., 2024) has become increasingly important because it better aligns with the practical needs of developers in real-world software development. However, repo-level code completion is a non-trivial task by considering the following situations:

As shown in Fig. 1 (s1), this example shows the LLM incorrectly focusing on method signatures in the local (i.e., in-file) context, while ignoring the broader global (i.e., cross-file) context. The LLM incorrectly invokes processData(product, quantity), which fails to reflect the intended action of placing an order. The proper call is processPurchase(product, quantity) (e.g., colored in green), which aligns with the intended purchase logic defined in the PurchaseProcessor class, imported from the cross-file context. This

*co-corresponding authors.

situation highlights that the LLM can't correctly focus on local context or global context. As shown in Fig. 1 (s2), this example shows an LLM's failure to deeply reason about the target cross-file object. The LLM incorrectly calls dbConnection.connect() directly after instantiated newUser neglecting the initialization logic newUser.save() encapsulated within the UserManager class, which internally manages the database interaction.

Therefore, a key research question regarding repo-level code completion stands out, i.e., how to improve the model's capability to utilize both cross-file and in-file context effectively? It is a challenging task: (i) Guiding LLMs to focus on the suitable context (e.g., in-file context or cross-file context or both) remains a non-trivial problem. Different parts of the codebase depend on vastly different types of contextual information. Some completions can be resolved using local (in-file) scope, while others require a broader, cross-file perspective. LLMs still lack the capability to decide which type of context is relevant for a given completion task (Ding et al., 2023). (ii) LLMs still struggle to reason deeply about the cross-file information due to complicated project dependencies and code structures. From the repository perspective, code completion requires the LLM to have a global view that spans multiple files and modules. This means a deep understanding of how different components are architected and interact.

To tackle the above challenges, we propose a novel reinforcement learning (RL) approach for repo-level code completion, which is designed to improve the model's completion ability in different completion scenarios. To address the challenge of focusing on a suitable context, we leverage identifier-driven intent recognition to capture the completion intent. Specifically, we adopt the GRPO (Shao et al., 2024) method for autonomous learning and design an intent reward based on the matching degree between identifiers in the predicted and real code, guiding the LLM to focus on the correct context. To address the challenges of dependency resolution and cross-file reasoning, we propose a reward-driven completion learning utilizing a reward function for repo-level code completion, providing continuous and granular reward values based on the distance of the code editing as feedback for cross-file reasoning. To guide LLMs to effectively complete code that meets the intended functionality and adheres to the repository-level context, we design a selective-exploration strategy optimization that focuses on low-confidence, high-reward tokens in the model's multiple responses, reflecting divergent thought paths and motivating the model to explore underexplored yet more valuable code completion patterns.

In summary, our paper makes the following contributions: (1) A reinforcement learning method for repo-level code completion, which effectively addresses the issue of rewards in complex repository completion scenarios and improves cross-file reasoning, enabling the model to learn autonomously on real-world repository data. (2) The first deep research into LLMs' multi-scenario code completion ability, enhancing the model's ability to focus on a suitable repository context through intent recognition and selective exploration. (3) IntentCoder achieves state-of-the-art (SOTA) performance on repo-level code completion tasks with code LLM (7B parameters) on RepoBench, demonstrating strong and stable improvements.

References (24)