RepoShapley: Shapley-Enhanced Context Filtering for Repository-Level Code Completion

Yu Huo, Kun Zeng, Siyu Zhang, Yuquan Lu, Cheng Yang, Yifu Guo, Xiaoying Tang

TL;DR

RepoShapley introduces a novel, coalition-aware context filtering framework for repository-level code completion using Retrieval-Augmented Generation (RAG). By leveraging Shapley values from cooperative game theory, it explicitly models how retrieved code chunks interact—capturing synergies and conflicts rather than evaluating chunks in isolation. This approach significantly improves code completion quality while reducing context overhead and eliminating unnecessary retrievals.

What Problem Does This Paper Address?

Large Language Models (LLMs) excel at generating code, but repository-level code completion requires understanding non-local dependencies like project-specific APIs, shared contracts, and custom utilities across multiple files. While RAG helps by injecting cross-file snippets into the LLM's context window, managing this context is notoriously difficult.

The core challenge is that a code chunk's utility is highly interaction-dependent. For example, an interface declaration might seem useless on its own, but it becomes critical when paired with its implementation (synergy). Conversely, a snippet containing a deprecated API might conflict with an updated one, actively harming the model's generation (interference). Existing context filters score chunks independently, failing to account for these combinatorial interactions and often leading to bloated context windows or the accidental removal of crucial information.

Methods

The authors propose RepoShapley, a two-stage framework consisting of an offline labeling pipeline and an online distilled controller:

  1. ChunkShapley (Offline Labeling): To avoid the prohibitive cost of evaluating an exponential number of chunk combinations directly on a large generator model, the authors introduce a lightweight logistic surrogate game. First, they probe each retrieved chunk individually using teacher-forced log-likelihood to get a baseline score. These scores are fed into the surrogate game to efficiently compute exact Shapley values for a small retrieval set ($K=10$). Finally, a bounded post-verification step grounds the selected chunk coalitions against the frozen generator's actual decoding behavior to produce optimal "keep" or "drop" labels.
  2. RepoShapley (Online Distillation): The verified keep/drop decisions and retrieval triggers are distilled into the generator model itself using discrete control tokens (<NEED>, <DONE>, <KEEP>, <DROP>). During inference, the model can decide in a single autoregressive rollout whether it needs to retrieve external context and exactly which chunks to retain, eliminating online combinatorial search overhead.
Comparison of independent filtering vs. coalition-aware RepoShapley

Datasets

  • Training Data: The authors curated a dataset of 290k Python repositories from The Stack, applying strict quality filtering (AST parsing, line length constraints, and deduplication). From this, they sampled 7.5k repositories to construct 50k highly reliable, labeled training instances.
  • Evaluation Benchmarks: RepoShapley was evaluated across three realistic repository-level code completion benchmarks: RepoEval, CrossCodeEval, and CrossCodeLongEval. These benchmarks cover line, API, chunk, and function-level completion tasks. The authors also extended evaluation to Java, C#, and TypeScript to demonstrate cross-lingual generalization.

Key Insights and Ideas

  • Context Selection as a Cooperative Game: Treating context filtering as a cooperative game allows the framework to naturally handle the nuances of code synergy and conflict. Chunks aren't just "good" or "bad"; their value depends on the team of chunks they are deployed with.
  • The Power of "Partial Credit": The authors found that using token-level log-likelihood as the utility function drastically outperformed discrete metrics like Exact Match (EM) or Edit Similarity (ES) during the offline phase. Log-likelihood captures subtle improvements in the model's understanding (like correctly predicting a variable type) even if the final greedy token output hasn't flipped yet.
  • Smart Pruning Beats Aggressive Pruning: Analysis of the retained context length showed that RepoShapley strikes a golden mean. It avoids the massive context bloat of unfiltered RAG while retaining weak but necessary dependencies that aggressive, independent filters (like CODEFILTER) accidentally discard.

Key Results

RepoShapley consistently achieved state-of-the-art performance across multiple model backbones (including StarCoder-Base 1B/3B/7B and CodeLlama 7B/13B) and all 11 tested evaluation metrics.

Figure 1: Performance radar charts on StarCoder-Base- 7B and CodeLlama-13B. The plots display relative im- provements over the No-Retrieve baseline (center). RE-
  • Accuracy Improvements: On StarCoder-Base-7B, RepoShapley boosted the RepoEval API completion Exact Match (EM) score from 53.62% (RepoFormer) and 51.97% (CODEFILTER) to 58.79%.
  • Retriever Agnostic: The framework's gains are robust to the choice of retriever; replacing a sparse Jaccard retriever with a dense UniXcoder retriever yielded comparable absolute performance gains (+7.69 Edit Similarity).
  • Modern LLM Compatibility: When used as an external selective-RAG policy for cutting-edge models like DeepSeek-R1, Qwen2.5-Max, and GPT-5, RepoShapley consistently improved both EM and ES scores over standard full-retrieval strategies.

Limitations and Future Work

Despite its strong performance, the authors note several limitations:

  • Higher-Order Interactions: Because the logistic surrogate game is initialized from single-chunk probes, it may still miss complex, higher-order interactions where multiple individually weak chunks only become useful when all of them are present simultaneously.
  • Scalability Bottleneck: Computing exact Shapley values requires enumerating $2^K$ subsets. While computationally negligible for $K=10$, this exponential growth limits the framework's scalability to much larger retrieval budgets.
  • Granularity and Decoding Ties: The framework relies on a fixed chunking granularity and ties its verification stage to greedy decoding metrics, meaning performance could vary under alternative decoding strategies like nucleus sampling.

Summary updated

BibTeX
@inproceedings{huo2026reposhapley,
  author = {Yu Huo and Kun Zeng and Siyu Zhang and Yuquan Lu and Cheng Yang and Yifu Guo and Xiaoying Tang},
  title = {{RepoShapley: Shapley-Enhanced Context Filtering for Repository-Level Code Completion}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2026},
  doi = {10.18653/v1/2026.findings-acl.505},
}
Figure