Abstract
Repository-level code generation aims to generate code within the context of a specified repository. Existing approaches typically employ retrieval-augmented generation (RAG) techniques to provide LLMs with relevant contextual information extracted from the repository. However, these approaches often struggle with effectively identifying truly relevant contexts that capture the rich semantics of the repository, and their contextual perspectives remains narrow. Moreover, most approaches fail to account for the structural relationships in the retrieved code during prompt construction, hindering the LLM's ability to accurately interpret the context. To address these issues, we propose RepoScope, which leverages call chain-aware multi-view context for repository-level code generation. RepoScope constructs a Repository Structural Semantic Graph (RSSG) and retrieves a comprehensive four-view context, integrating both structural and similarity-based contexts. We propose a novel call chain prediction method that utilizes the repository's structural semantics to improve the identification of callees in the target function. Additionally, we present a structure-preserving serialization algorithm for prompt construction, ensuring the coherence of the context for the LLM. Notably, RepoScope relies solely on static analysis, eliminating the need for additional training or multiple LLM queries, thus ensuring both efficiency and generalizability. Evaluation on widely-used repository-level code generation benchmarks (CoderEval and DevEval) demonstrates that RepoScope outperforms state-of-the-art methods, achieving up to a 36.35% relative improvement in pass@1 scores.
Introduction: The Challenge of Repository Context
Generating code for a single function is a well-solved problem for modern Large Language Models (LLMs). However, generating code that integrates seamlessly into a large, complex software repository remains a significant challenge. LLMs face two primary hurdles in this domain: constrained context windows and a lack of project-specific knowledge regarding internal APIs, custom classes, and coding conventions.
Current solutions typically rely on Retrieval-Augmented Generation (RAG), which searches the repository for code snippets textually similar to the target task. However, textual similarity is often a noisy signal. As shown in Figure 1, a model might retrieve functions that look similar but fail to identify the specific internal helper methods (callees) required for the correct implementation. This leads to "bad solutions" that use incorrect or non-existent API calls.
The RepoScope Framework
To address these limitations, we introduce RepoScope, a framework that moves beyond simple text search by leveraging the structural semantics of the repository. RepoScope operates through three core stages: Indexing, Retrieval, and Generation.
1. Repository Structural Semantic Graph (RSSG)
RepoScope begins by performing static analysis on the entire repository to construct a heterogeneous graph, the RSSG. This graph maps the relationships between various entities such as classes, functions, and attributes. It captures dependencies like "Class A contains Function B" or "Function C returns an instance of Class D," providing a rich structural map of the codebase.
2. Call Chain Prediction
A key innovation of RepoScope is its ability to "guess" the functions the target code will likely call before it is even written. By analyzing call patterns of similar existing functions and performing a Depth-First Search (DFS) on the RSSG, RepoScope identifies likely sequences of calls—call chains. This ensures the LLM is provided with the specific internal "tools" it needs.
3. Structure-Preserving Serialization
Standard RAG methods often provide the LLM with a "flat" list of code snippets, which destroys hierarchical relationships. RepoScope employs a tree-traversal algorithm to format the retrieved context, maintaining the original indentation and class-method nesting. This structure-preserving serialization makes it significantly easier for the LLM to interpret the relationships between different code elements.
Evaluation and Results
The researchers evaluated RepoScope using three major benchmarks: CoderEval (207 Python tasks), DevEval (1,742 samples from 117 real-world repositories), and RepoEval (API-level completion).
RepoScope consistently outperformed state-of-the-art baselines like DRACO and RepoCoder across various backbone models, including GPT-4o mini, Claude-3.5-Haiku, Qwen2.5, and DeepSeek-V3. Key findings include:
- Significant Accuracy Gains: On the DevEval benchmark using Claude-3.5-Haiku, RepoScope achieved a 36.35% relative improvement in pass@1 scores.
- Token Efficiency: RepoScope often achieved superior results using fewer input tokens, demonstrating that the quality and structure of context are more critical than sheer volume.
- Robust Generalization: The framework showed a 7.33% improvement in Exact Match scores on API-level generation tasks, proving the utility of the RSSG approach beyond its primary design.
Conclusion
RepoScope demonstrates that structural semantics are a powerful tool for repository-level code generation. By combining static analysis with call chain prediction and structure-preserving prompt construction, it provides LLMs with a high-fidelity view of the codebase. Because it relies on single-pass static analysis rather than expensive iterative LLM queries, RepoScope is both efficient and well-suited for production environments. Future work will focus on handling highly dynamic language features and integrating RepoScope into broader agentic frameworks.
Summary updated