TL;DR
The paper introduces DyRetriever (integrated into the DyCoder framework), a novel context retrieval method for repository-level code generation that mimics how human developers explore codebases. Instead of relying on rigid text-similarity or expensive, static global dependency graphs, it leverages a Large Language Model (LLM) to dynamically build and traverse a partial dependency graph on demand. This approach yields up to a 59.73% relative improvement in code generation accuracy while running 7.4x faster than traditional graph-based baselines.
What Problem Does This Paper Address?
When generating code within a large software repository, LLMs face a major bottleneck: they cannot fit an entire codebase into their limited context window, nor do they inherently understand complex cross-file dependencies. Traditional Retrieval-Augmented Generation (RAG) fixes this by pulling in relevant code snippets. However, standard RAG relies on text similarity, which frequently misses critical helper functions that don't look like the target function but are functionally required.
To fix this, recent advanced methods build global dependency graphs using static analysis tools. However, constructing these global graphs is incredibly time-consuming (often taking hours for large codebases) and expensive to maintain as the repository continuously evolves. The authors address this dilemma: How can we efficiently retrieve structurally relevant code context without the massive overhead of a global dependency graph?
Methods
The authors propose DyCoder, a framework that combines standard similarity-based dense retrieval with a novel graph-based retriever called DyRetriever.
Instead of pre-computing a massive global graph, DyRetriever mimics human behavior by building a partial dependency graph on the fly through the following steps:
- Entry Points Selection: The LLM reviews the high-level repository structure and selects a few files and functions likely to be relevant to the target function's signature.
- Multi-Hop Reasoning: Starting from these entry points, the LLM iteratively explores the codebase. At each step, it decides which function to visit, inspects its code, extracts import statements, and dynamically expands the local graph by identifying downstream callees.
- Validation & Post-Processing: To combat LLM hallucinations, DyRetriever verifies that the discovered dependency functions actually exist in the repository; if no implementation is found, they are discarded.
Finally, the retrieved structural context is merged with similarity-based context, deduplicated, and fed to the LLM to generate the completed function.
Datasets
The authors evaluated their approach using two prominent repository-level code generation benchmarks for Python:
- CoderEval-Python: Contains 230 instances designed to test code generation capabilities across six distinct levels of contextual dependency.
- DevEval: A large, manually annotated benchmark containing 1,825 instances extracted from 117 real-world codebases, offering a more complex and realistic evaluation environment.
Key Insights and Ideas
- Human-centric Exploration: Human developers don't map out an entire 100,000-line codebase before writing a function; they follow a trail of breadcrumbs (imports and function calls) on demand. LLMs can successfully replicate this exact mental model.
- Similarity $\neq$ Dependency: Functions with entirely different semantic descriptions can be strictly dependent on one another. For example, a path iteration function might heavily rely on a path normalization function, even if their text embeddings do not closely align.
- Local vs. Global Accuracy: While global static analysis for dynamic languages like Python is notoriously difficult and error-prone, LLMs are surprisingly accurate (93.22%) at identifying downstream dependencies (callees) when looking at a single function locally.
Key Results
- Accuracy Boost: DyCoder achieved substantial relative improvements in Pass@1 accuracy over state-of-the-art RAG baselines: a 25.63% average improvement on CoderEval and a massive 59.73% average improvement on the more complex DevEval benchmark.
- 7.4x Speedup: By avoiding global graph construction, DyCoder cuts indexing and retrieval times dramatically, running 7.4 times faster than RepoScope (the leading static graph baseline).
- Cost-Effective: Despite requiring multiple LLM calls to traverse the partial graph, the average token inference cost remains incredibly low at just $0.0039 per generated function.
Limitations and Future Work
- Language Constraints: The current implementation and evaluation are limited strictly to Python. While Python's dynamic nature makes dependency tracking harder (proving the robustness of the method), evaluating on statically typed languages like Java or C++ remains future work.
- Task Complexity: The framework was tested primarily on generating single target functions. The authors note that extending this to more complex repository-level tasks, such as multi-function feature additions, is an open area of exploration.
- Graph Accuracy Scaling: Though highly accurate, the LLM can still miss dependencies or hallucinate when dealing with exceptionally long functions (spanning hundreds of lines) or functions with more than 10 dependencies.
Summary updated