Introduction
Large Language Models (LLMs) have revolutionized code completion at the function level. However, real-world software development occurs within large, multi-module repositories where code in one file frequently depends on definitions across many others. In these complex environments, standard LLMs often struggle to navigate intricate cross-file dependencies—a phenomenon known as the "lost in the codebase" problem.
The Challenge of Repository-Scale Completion
Current state-of-the-art approaches typically employ Retrieval-Augmented Generation (RAG) to provide context. These methods search the codebase for relevant snippets based on textual similarity and append them to the model's prompt. However, this approach suffers from two critical limitations:
- Textual Myopia: Retrieval relies on keywords or embeddings that capture what code looks like, often ignoring the structural "skeleton" of the repository, such as call chains or inheritance hierarchies.
- Structural Loss: When retrieved snippets are flattened into a text list, the explicit links between them are lost. The LLM must then guess how a retrieved class definition relates to the code currently being written.
The GRACE Framework
To address these issues, we introduce GRACE (Graph-Guided Repository-Aware Code Completion). GRACE reimagines the code repository as a hierarchical, semantically rich graph rather than a flat collection of files.
1. Multi-level Code Graph Construction
GRACE builds a unified graph representation across three distinct granularities:
- Repository Level: Captures folder structures and cross-file dependencies.
- Module Level: Maps function call graphs and class inheritance trees.
- Function Level: Analyzes Abstract Syntax Trees (AST), Control Flow Graphs (CFG), and Data Flow Graphs (DFG).
2. Hybrid Graph Retriever (HGR)
The framework utilizes a dual-path retrieval strategy. A Semantic Path uses traditional embeddings to find functionally similar code, while a Structural Path employs Graph Neural Networks (GNNs) to identify code with similar topological patterns. A specialized Graph-Aware Reranker then selects the most relevant subgraphs for the current context.
3. Hierarchical Code Fusion
The core innovation of GRACE is Graph Fusion. Instead of simple text concatenation, GRACE identifies "cross-graph edges" between the user's local code and the retrieved subgraphs. This fused structure is serialized into a specialized format that explicitly defines relationships (e.g., variable-class instances), providing the LLM with a clear "map" of dependencies.
Evaluation and Results
The researchers evaluated GRACE using two major benchmarks: CrossCodeEval, which tests cross-file reasoning across multiple languages, and RepoEval-Updated, a dataset of recent repositories designed to prevent data leakage from the LLMs' training sets.
Key findings include:
- Performance Gains: Using DeepSeek-V3, GRACE outperformed strong graph-based RAG baselines by 8.19 points in Exact Match (EM) and 7.51 points in Edit Similarity (ES).
- Scaling Synergy: The benefits of GRACE increase with model size. Larger models are significantly better at interpreting the serialized graph structures provided by the framework.
- Ablation Insights: Removing the "Fusion" component led to the most significant performance drops, confirming that explicit structural linking is as vital as the retrieval itself.
Conclusion
GRACE demonstrates that providing LLMs with a structural "map" of a codebase significantly enhances their ability to perform complex, repository-aware code completion. By moving beyond textual similarity to hierarchical graph fusion, GRACE sets a new standard for AI-assisted software development in large-scale projects. Future work will focus on incremental indexing for massive repositories and real-time graph updates during active development.
Summary updated