NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

RANGER -- Repository-Level Agent for Graph-Enhanced Retrieval

Pratik Shah, Rajat Ghosh, Aryan Singhal, Debojyoti DuttaarXiv 2025

TL;DR

RANGER is a repository-level code retrieval agent designed to handle both precise code-entity lookups and open-ended natural language queries. By combining an Abstract Syntax Tree (AST)-parsed knowledge graph with a Monte Carlo Tree Search (MCTS) traversal algorithm, RANGER successfully navigates complex cross-file dependencies to outperform state-of-the-art embedding models in code search and completion tasks.

What Problem Does This Paper Address?

Modern software engineering agents rely heavily on Retrieval-Augmented Generation (RAG) to fetch context from large codebases. However, standard RAG systems struggle with code for two main reasons:

  1. Structural Complexity: Code isn't flat text. It contains long-range, multi-hop, and cross-file dependencies where a function's behavior might depend on an import or variable defined miles away.
  2. Query Diversity: Developers query codebases in two distinct ways. They might use code-entity queries (e.g., "What are the dependencies of the Calculator class?") or natural language queries (e.g., "Where do we implement addition?").

While existing graph-based retrieval methods excel at structural, entity-based lookups, they falter on open-ended natural language queries due to the semantic gap between natural language and symbolic code. RANGER bridges this gap by providing a unified, dual-stage retrieval pipeline.

Methods

RANGER operates in two distinct phases: an offline indexing stage and an online query stage.

Figure 1: RANGER system architecture illustrated through a simple two-file repository example containing base.py and extended.py. The offline stage constructs a comprehensive knowl- edge graph from code repositories through AST parsing, LLM-assisted semantic description gener- ation, and embedding computation. In the online stage RANGER first translates user queries into Cypher queries using an LLM. For code-entity queries, this Cypher query is sufficient and provides fast retrieval (Path 1). If retrieval results from the graph database return None, often in case of natural language queries, the system invokes MCTS-based graph exploration (Path 2) to generate the final response.

1. Offline Knowledge Graph Construction

  • File-Level Parsing: RANGER uses the tree-sitter library to parse source files into detailed ASTs, extracting fine-grained entities like Module, Class, Function, Method, Field, and GlobalVariable.
  • Repository Consolidation: It maps out intra-file hierarchies and explicitly resolves cross-file dependencies by "stitching" temporary Import nodes to their actual target entities across the repository.
  • Semantic Augmentation: To bridge the text-code gap, an LLM generates hierarchical natural language descriptions (purpose and member summaries) for each node, which are then converted into vector embeddings.

2. Online Router Retrieval Agent

When a user submits a query, RANGER first uses an LLM to translate it into a graph-database Cypher query:

  • Path 1 (Code-Entity Queries): If the query targets specific code identifiers, the Cypher lookup succeeds, providing fast, direct retrieval.
  • Path 2 (Natural Language Queries): If the Cypher lookup returns no direct matches (common for vague natural language descriptions), RANGER routes the query to its MCTS-based graph traversal algorithm.

MCTS-Guided Graph Exploration

The MCTS algorithm balances exploration of new graph regions with exploitation of known high-reward paths through four steps:

  1. Selection: Navigates the search tree using the Upper Confidence bound for Trees (UCT) score.
  2. Expansion: Uses a computationally cheap bi-encoder to rank unvisited neighbors by cosine similarity, adding the top-$k$ candidates to the tree.
  3. Simulation: Instead of random rollouts (which fail in code retrieval), RANGER uses a highly accurate cross-encoder to directly score the relevance of the expanded nodes against the query, serving as the search reward.
  4. Backpropagation & Extraction: Propagates rewards back up the tree and extracts a final ranked list of code snippets.
Figure 2: The Monte Carlo Tree Search–based graph traversal algorithm. The diagram depicts iterations 2 and 3 of a Monte Carlo Tree Search–based graph traversal on the simple two-file code repository knowledge graph from Figure 1 in response to the query “Where is the code for addition?” Iteration 2 expands the base module (adding precision and Calculator), simulates their rewards, and back-propagates values to update selection scores. Iteration 3 then adds the method node add and function node quick add, both of which yield high rewards and are ultimately selected during the extraction and ranking phase as the answer to the user’s query.

Datasets

The authors evaluated RANGER across four diverse benchmarks representing core automated software engineering tasks:

  • CodeSearchNet (Python split): 99 natural language queries with expert relevance annotations over a large corpus of functions.
  • RepoQA (Python split): A modified "Searching Needle Function" task evaluating long-context code understanding.
  • RepoBench (RepoBench-R): Evaluates cross-file snippet retrieval for next-line code prediction.
  • CrossCodeEval (Python split): Tests repository-level code completion where continuations depend on cross-file context.

Key Insights and Ideas

  • Making Cross-Encoders Affordable: Cross-encoders are highly accurate for re-ranking but computationally prohibitive to run across an entire codebase. By embedding the cross-encoder strictly within the MCTS simulation phase, RANGER only evaluates high-value candidates, keeping high-accuracy retrieval computationally tractable.
  • Structural Traversal over Semantic Proximity: In repository-level code completion, the code you need often lives in a completely different file and may not share semantic text similarity with your current cursor context. RANGER succeeds because it follows explicit structural relationships (like USES or INHERITS) rather than relying solely on embedding proximity.

Key Results

Natural Language Query Performance

On CodeSearchNet and RepoQA, RANGER consistently outperformed strong vector search baselines, even beating retrieval pipelines utilizing state-of-the-art embeddings like Qwen-3-8B.

Performance comparison on CodeSearchNet and RepoQA datasets.

As shown below, performance metrics scale monotonically with the number of MCTS iterations before converging, offering a clear knob to balance retrieval quality against computational latency.

Figure 3: Performance metrics across MCTS iterations for natural language query datasets. Left shows CodeSearchNet NDCG@10, Recall@10, and the average MCTS runtime per iteration across repositories and queries. Right shows RepoQA NDCG@10, Recall@10, and corresponding average runtimes. Both datasets show monotonic improvement followed by convergence, indicating practical iteration ranges for deployment.

Code-Entity Query Performance

  • RepoBench: RANGER demonstrated superior localization of fine-grained dependencies compared to standard file-level import baselines across multiple re-rankers.
Table 2: Performance comparison on the RepoBench benchmark for cross-file dependency retrieval.
  • CrossCodeEval: When paired with BM25 (combining graph-based cross-file retrieval with sparse lexical search), RANGER achieved the highest Exact Match (EM) rate for code completion when paired with DeepSeek-Coder-7B (36.27% vs BM25's 28.57%) and CodeLlama-7B.

Limitations and Future Work

  • Static Graphs: RANGER relies on an offline-constructed static graph, making it less suitable for rapidly evolving codebases where dependencies change in real-time.
  • Inference Latency: The MCTS exploration stage introduces additional computational overhead and latency, which could hinder interactive, real-time developer workflows.
  • Reward Signal Dependency: Node scoring currently relies entirely on cross-encoder relevance estimates, which may not perfectly capture the optimal reward signal for complex multi-hop reasoning.

Future Directions: The authors plan to explore incremental graph maintenance for live updates, integrate a multi-stage ReACT-style agent architecture to reduce rollout depth, and extend tree-sitter parsing to support languages beyond Python.

Summary updated

BibTeX
@misc{shah2025ranger,
  author = {Pratik Shah and Rajat Ghosh and Aryan Singhal and Debojyoti Dutta},
  title = {{RANGER -- Repository-Level Agent for Graph-Enhanced Retrieval}},
  year = {2025},
  eprint = {2509.25257},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.25257},
}
Figure