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:
- 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.
- 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.
1. Offline Knowledge Graph Construction
- File-Level Parsing: RANGER uses the
tree-sitterlibrary to parse source files into detailed ASTs, extracting fine-grained entities likeModule,Class,Function,Method,Field, andGlobalVariable. - Repository Consolidation: It maps out intra-file hierarchies and explicitly resolves cross-file dependencies by "stitching" temporary
Importnodes 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:
- Selection: Navigates the search tree using the Upper Confidence bound for Trees (UCT) score.
- Expansion: Uses a computationally cheap bi-encoder to rank unvisited neighbors by cosine similarity, adding the top-$k$ candidates to the tree.
- 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.
- Backpropagation & Extraction: Propagates rewards back up the tree and extracts a final ranked list of code snippets.
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
USESorINHERITS) 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.
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.
Code-Entity Query Performance
- RepoBench: RANGER demonstrated superior localization of fine-grained dependencies compared to standard file-level import baselines across multiple re-rankers.
- 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