Organizations: MIIT Key Laboratory of Pattern Analysis and Machine Intelligence, College of Computer Science and Technology, Nanjing University of Aeronautics and Astronautics · Tsinghua University
Retrieval-Augmented Generation (RAG) has advanced repository-level code completion by retrieving external code snippets, API documentation, or type definitions to expand the model's contextual awareness. However, under the finite token budget of the context window, existing retrieval-augmented methods often saturate the budget with highly similar or even redundant code, which not only wastes tokens but also suppresses critical cross-file dependencies, preventing synergistic interaction between semantically similar reference snippets and cross-file dependency definitions. Therefore, further gains are limited by low knowledge density in the retrieved context rather than by model capacity. We propose SaraCoder, a retrieval-augmented framework that enhances knowledge density via hierarchical context pruning under a fixed context budget. A Hierarchical Feature Optimization module prunes an over-complete candidate pool in four stages. The freed tokens are then allocated to an External-Aware Identifier Disambiguator, which selectively injects only the most necessary cross-file dependency definitions. SaraCoder transforms context construction into a budget-aware optimization problem, balancing reference snippets and cross-file dependencies to build a context that is both knowledge-dense and semantically complete. Experiments on CrossCodeEval and RepoEval-Updated, across multiple backbone code LLMs, show that SaraCoder consistently outperforms strong RAG baselines such as GraphCoder and Repocoder in both code-level and identifier-level metrics, without increasing token usage, and yields synergistic gains when combined with other cross-file methods.
Figures & tables
Fig. 1: The pitfalls of pure similarity retrieval vs. the efficiency of Saracoder . Pink boxes highlight how traditional retrieval leads to “Crowding out” the context with redundant semantics or ambiguous noise. In contrast, green boxes showcase the “Compact & Inject” strategy of Saracoder , which distills essential knowledge and injects precise, high-value insights.
Fig. 2: An illustration of Saracoder framework. (1) Database Construction. This phase constructs a key-value codebase. This involves using a slicing algorithm to create induced graph slices, which are then precisely mapped to source code snippets. (2) Code Retrieval. This phase takes code context as input and retrieves similar code, then refines suggestions via Hierarchical Feature Optimization. Concurrently, an External-Aware Identifier Disambiguator clarifies external symbols via dependency analysis, delivering highly accurate candidates. (3) Code Generation. This phase generates prompts by integrating outputs from code retrieval with the code completion context. These prompts are then fed into an LLM to predict completion statements.
Fig. 3: Prompt template. ( Sref corresponds to similar code snippet, Dext corresponds to external symbols enhancement, and Ccontext corresponds to unfinished code context.)
CrossCodeEval
RepoEval-Updated
Python
Java
Python
Java
Total Repositories
471
239
10
8
Total Files
14348
5868
3258
8260
Total Task cases
2665
2139
2000
1600
TABLE I: CrossCodeEval vs. RepoEval-Updated.
Relationship
Definition and Type-Sensitive Characteristics
Syntax Examples (Python / Java)
Assignment
A variable acquires its type identity through assignment, which enables type inference and propagation.
Python: count: int = 10 ; Java: String s = new String() .
Contextual Binding
Temporary type bindings are created within specific syntactic structures, and their lifecycle is managed depending on the context.
Python: with open(file) as f: ; Java: try (BufferedReader br = ...) .
Reference
Existing variables or properties are accessed, which requires late-bound type resolution.
Python: obj.calculate() ; Java: this.value .
Type Declaration
Variable or return types are explicitly annotated, which provides the basis for static type checking.
Python: def func() -> list[str]: ; Java: List<Integer> list = new ArrayList<>() .
Parameter Constraint
Function parameters are constrained by types, which ensures input type validation.
Function return values are constrained by types, which guarantees output type consistency.
Python: @return_type(float) ; Java: public int getValue() { ... } .
TABLE II: Key Associations for Analysis, Refactoring, and Debugging Modeled in Context Graph Construction
Dataset
Language
Methods
Models and Metrics
Codegen2-7b
Codegen25-7b
deepseek-coder-6.7b-instruct
CodeLlama-7b-Instruct
Code Match
Identifier Match
Code Match
Identifier Match
Code Match
Identifier Match
Code Match
Identifier Match
EM
ES
EM
F1
EM
ES
EM
F1
EM
ES
EM
F1
EM
ES
EM
F1
RepoEval-Updated
Python
No Rag
17.40
32.54
23.75
30.21
19.55
34.48
25.75
32.16
11.50
30.33
15.30
22.39
17.35
33.05
23.55
30.53
Shifted Rag
32.70
59.22
40.10
55.66
36.45
61.96
43.20
58.31
20.90
42.95
26.50
38.88
33.90
60.28
41.50
56.39
Vanilla Rag
38.70
63.58
46.45
60.43
42.25
66.26
48.75
62.79
22.20
41.48
27.85
37.58
40.30
65.03
47.55
61.06
TABLE III: Performance comparison on RepoEval-Updated and CrossCodeEval datasets. Numbers are shown in percentage (%). The top results are bolded , and the second best are underlined .
Fig. 4: Impact of top_k on RepoEval-Updated and CrossCodeEval.
Language
Methods
Codegen2-7b
Codegen25-7b
CodeLlama
Code Match
Identifier Match
Code Match
Identifier Match
Code Match
Identifier Match
EM
ES
EM
F1
EM
ES
EM
F1
EM
ES
EM
F1
Python
No Rag
5.44
57.85
11.71
42.22
7.77
60.52
14.45
45.40
9.49
61.97
16.44
47.36
Shift Rag
4.87
58.36
11.64
42.91
7.44
60.20
14.17
44.78
6.95
60.35
13.75
45.36
Vanilla Rag
9.52
61.87
17.42
48.01
12.43
63.81
20.74
51.00
11.48
63.66
19.42
50.72
Saracoder
12.16
54.16
18.37
45.47
11.50
44.90
16.22
38.95
13.32
48.04
19.54
42.34
TABLE IV: Performance benefits of Saracoder when integrated orthogonally with other cross-file approaches ( % ). The CrossCodeEval dataset is used in this part.
Method
Codegen2-7b
deepseek-coder-6.7b-instruct
CodeLlama-7b-Instruct
#In
#Out
#In
#Out
#In
#Out
No RAG
1397.38
50.44
1383.12
95.22
1388.38
50.146
Shifted RAG
821.627
95.15
778.52
93.48
763.93
96.86
HF_OP (top_k=10)
1665.50
88.15
1621.92
90.86
1611.37
96.99
HF_OP (top_k=4)
1644.26
89.20
1596.83
88.57
1590.55
98.06
TABLE V: Quantitative analysis of top_k and token when max_num_tokens is 2048.
Dataset
Language
Method
hit@1
hit@5
hit@10
RepoEval_Updated
python
No Comp.
25.40%
32.15%
34.35%
HF_OP
25.00%
34.50%
36.45%
java
No Comp.
12.69%
19.81%
22.25%
HF_OP
15.12%
21.94%
23.19%
CrossCodeEval
python
No Comp.
0.11%
0.26%
0.38%
HF_OP
2.61%
3.97%
4.16%
TABLE VI: Hit@k Performance on RepoEval_Updated and CrossCodeEval Datasets. “No Comp.” denotes the original retriever without any compression processing.
Fig. 5: Ablation study. (Each three-data-point group represents CodeGen2-7B, CodeGen2.5-7B, and CodeLlama-7B-Instruct models. Bar lengths show their average performance, with I-shaped error bars indicating standard deviation)
LLM-based repository-level code generation aims to generate code using the context available in a software repository, requiring LLMs to reason over complex code dependencies. Due to limited context windows and insufficient repository-specific understanding, LLMs typically rely on retrieval-augmented generation (RAG) to incorporate relevant code. Early RAG approaches primarily employ similarity-based retrieval, which often fails to retrieve code snippets that the target function depends on. Recent work introduces graph-based retrieval to model such dependencies, but typically relies on manually designed rules and static global graphs, leading to limited flexibility and high construction and maintenance costs. In contrast, human developers collect helpful context by implicitly constructing a partial dependency graph and iteratively inspecting along it. Inspired by this behavior, we propose DyRetriever, an efficient context retrieval method via partial dependency graphs. DyRetriever uses an LLM to first select a set of entry-point functions and then perform multi-hop reasoning along the code dependency graph. During multi-hop reasoning, it uses the LLM's semantic understanding to validate whether a function can help generate the target function, eliminating manually designed rules and enabling flexibility across scenarios. Instead of statically constructing a global dependency graph, DyRetriever builds a partial graph on demand and discards it after use, reducing construction and maintenance costs. We integrate DyRetriever with a similarity-based code retriever to build DyCoder and evaluate it on CoderEval and DevEval. Experimental results show that DyCoder achieves relative Pass@1 improvements of 25.63% and 59.73% on CoderEval and DevEval, respectively, compared with existing RAG-based methods, while being 7.4x faster than baselines based on static dependency graph construction.
Zhongxin Liu, Zhonghao Jiang, Zhifan Ye +3
College of Computer Science and Technology and the State Key Laboratory of Blockchain and Data Security, Zhejiang University · School of Computer and Computing Science, Hangzhou City University · Faculty of Computing, Harbin Institute of Technology +1
Context: Retrieval-augmented code generation relies on cross-file repository context, but retrieved snippets may come from obsolete project states. Objectives: We study whether temporally stale repository snippets act as harmless noise or actively induce current-state-incompatible code. Methods: We conduct a controlled diagnostic study on a curated 17-sample set of production-helper signature changes from five Python repositories. For each sample, we compare current-only, stale-only, no-retrieval, and mixed current/stale retrieval conditions under prompts that hide commit freshness and expected current signatures. Results: Under neutralized prompts, stale-only retrieval induces stale helper references on 15/17 Qwen2.5-Coder-7B-Instruct samples and 13/17 gpt-4.1-mini samples, corresponding to 88.2 and 76.5 percentage-point increases over current-only retrieval. No retrieval produces zero stale references but only 1/17 passing completions. The two models share 75.0% Jaccard overlap among stale-triggering samples, and mixed conditions show that adding valid current evidence largely rescues stale-only failures. Conclusion: Temporal validity of retrieved repository context is a distinct diagnostic variable for Code RAG robustness: stale context can actively bias models toward obsolete repository state rather than merely removing useful evidence.
Haojun Weng, Qianqian Yang, Hao Fu +2
Independent Researcher, California, USA · Independent Researcher, Beijing, China
Understanding large, complex, and rapidly evolving code bases remains a core challenge for software engineering at scale, hindering onboarding, maintenance, refactoring, and impact analysis. Providing this information to a Large-Language Model (LLM) is another hurdle, as the LLM must understand not only the required code method but also its associated methods in the grand scheme of the repository. We propose Code Comprehensor, a hybrid retrieval-reasoning framework combining (i) CodeGraph, a code dependency callgraph, (ii) CodeScribe, which uses code-aware LLMs to generate hierarchical documentation across repository call graphs, and (iii) CodeContrast, a CLIP-style dual-encoder model trained on CodeScribe output to learn aligned text-code embeddings. CodeContrast enables fast, semantically precise retrieval of relevant code-context neighborhoods, which serve as grounded evidence for an MCP-based LLM orchestrator answering domain-specific queries. Evaluation on OpenCV and a proprietary 5M-LOC repository shows that CodeScribe-derived supervision significantly improves cross-modal retrieval in text-to-code accuracy over baseline approaches. Richer textual supervision (hierarchical documentation, parent/sibling context, and LLM-generated synthetic questions) consistently enhances performance across both repositories. LLM-as-a-judge evaluation using the popular RAGAS of the Code Comprehensor output yields above 90% faithfulness scores.
Sujoy Roychowdhury, Joseph H. R. Isaac, Jiangning Gao +2