Introduction
Large Language Models (LLMs) have revolutionized standalone code generation, yet they struggle within the context of large-scale software repositories. Real-world engineering involves complex, cross-file dependencies that often exceed the context windows of most models. While Retrieval-Augmented Generation (RAG) is commonly used to bridge this gap by fetching relevant snippets, standard approaches often treat code as plain text. This oversight leads to "semantic gaps" and "structural mismatches," where retrieved code looks similar but fails to capture the underlying logic.
To address these challenges, we introduce CoCo (Completion by Comprehension), a framework designed to prioritize deep code understanding over simple text retrieval. By leveraging static analysis and graph-based selection, CoCo provides LLMs with high-signal, structurally relevant context.
The CoCo Framework
CoCo operates on the philosophy of "Comprehension first, then Completion." The framework integrates three core components to refine the context provided to the LLM:
1. Multi-Granularity Code Comprehension
Rather than treating the codebase as a flat text file, CoCo uses static analysis (e.g., Tree-sitter) to parse the environment at three distinct levels:
- Function-level: Maps execution logic via Control Flow Graphs (CFG) to understand the immediate context of the unfinished line.
- File-level: Tracks both explicit dependencies (active variables/functions) and "potential dependencies"—entities defined or imported but not yet utilized, which serve as strong hints for future code.
- Project-level: Analyzes cross-file module references and external library imports to maintain global awareness.
2. Graph-based Context Selection
To avoid overwhelming the LLM with noise, CoCo constructs a heterogeneous semantic graph of extracted dependencies. It employs Personalized PageRank (PPR), centered on the target function, to score and filter nodes. This ensures that only the most structurally significant context survives the prompt truncation process.
3. Structure-Aware Code Re-Ranker
CoCo enhances traditional RAG by adding a re-ranking layer. By extracting Abstract Syntax Tree (AST) paths from both the query and candidate snippets, the system calculates a Jaccard similarity score. This ensures that retrieved examples match the structural intent of the unfinished code, not just its surface-level vocabulary.
Evaluation and Results
The framework was evaluated using the CrossCodeEval and RepoEval benchmarks across several models, including DeepSeekCoder-1B, CodeLlama-7B, and Yi-Coder-1.5B.
Performance Gains
CoCo demonstrated significant improvements across all metrics. Notably, using Yi-Coder-1.5B, the framework achieved a 20.2% relative improvement in Exact Match (EM) on the CrossCodeEval Python benchmark. It consistently outperformed state-of-the-art baselines like RepoCoder and RLCoder in Edit Similarity and F1 scores.
Key Insights
- The Power of Potential Dependencies: Tracking "unused" imports proved to be a critical signal. Entities imported but not yet used are highly likely to appear in the next lines of code—a nuance standard RAG misses.
- Project-Level Context: Ablation studies confirmed that cross-file awareness is the primary bottleneck in repository-level completion; its inclusion provided the most substantial performance boost.
- Operational Efficiency: Despite the added complexity of static analysis, CoCo is highly efficient, adding less than 5% overhead to inference latency.
Conclusion and Future Work
CoCo offers a plug-and-play solution to enhance code generation by grounding LLMs in the structural reality of a codebase. While currently reliant on rule-based static analysis, which may struggle with highly dynamic logic, the framework sets a new standard for context-aware code completion. Future iterations may explore LLM-based intelligent summarization of context and more adaptive strategies for entity extraction.
Summary updated