Think-Search-Patch: A Retrieval-Augmented Reasoning Framework for Repository-Level Code Repair

Bojian Xiong, Yikun Lei, Xikai Liu, Shaowei Zhang, Pengcheng Zhu, Yan Liu, Yongqi Leng, Ling Shi, Meizhi Zhong, Yurong Zhang, Yan Gao, Yiwu, Yao Hu, Deyi Xiong2025

Abstract

Bojian Xiong, Yikun Lei, Xikai Liu, Shaowei Zhang, Pengyun Zhu, Yan Liu, Yongqi Leng, Ling Shi, Meizhi Zhong, Yurong Zhang, Yan Gao, Yiwu, Yao Hu, Deyi Xiong. Proceedings of the 2025 Conference on Empirical Methods in Natural Language Processing: Industry Track.

2025.

Introduction

Large Language Models (LLMs) have demonstrated exceptional capabilities in generating isolated code snippets. However, applying these models to repository-level code repair—where bugs are often buried within massive, multi-file software projects—remains a significant challenge. The primary obstacles are "context bloat," where irrelevant code obscures the bug, and the complex inter-dependencies between files that require deep semantic understanding.

To address these issues, researchers from Tianjin University and Xiaohongshu Inc. have introduced Think-Search-Patch (TSP). TSP is a retrieval-augmented reasoning framework designed to navigate repository complexities by decomposing the repair process into three distinct stages: reasoning, fine-grained retrieval, and standardized patching.

The TSP Framework

The TSP framework moves away from monolithic code generation in favor of a structured, sequential pipeline. This approach ensures that the model maintains a clear reasoning path from the initial bug report to the final code fix.

The three-stage TSP pipeline: Think (reasoning), Search (fine-grained retrieval), and Patch (generation).
  1. Think Stage: The model begins by analyzing the issue description. Rather than immediately searching for code, it performs a semantic analysis to decompose the problem into sub-tasks and constructs a precise search query. This "reasoning-first" approach ensures that the subsequent retrieval is targeted and relevant.
  2. Search Stage: Using the queries from the Think stage, the system retrieves relevant code snippets. A key innovation here is the use of fine-grained indexing. By utilizing an Abstract Syntax Tree (AST), TSP indexes code at the class and function levels rather than the file level. This reduces the average recalled context length by approximately 78%, effectively filtering out the "noise" of irrelevant boilerplate code.
  3. Patch Stage: In the final stage, the model uses the retrieved context to localize the flaw and generate a standardized patch in a diff format.

Specialized Training Strategy

The effectiveness of TSP is driven by a two-stage training process designed to optimize the model's collaborative reasoning:

  • Supervised Fine-Tuning (SFT): The researchers developed a custom 60,000-sample dataset (the TSP Dataset) to train models to follow the three-stage workflow. This dataset was built by augmenting existing code-repair data with explicit search modules.
  • Direct Preference Optimization (DPO): To minimize errors in intermediate reasoning (such as ineffective search queries), the team employed GPT-4o as a judge to identify and correct successful reasoning paths. DPO was then used to train the model to prefer these successful paths, enhancing the reliability of the entire pipeline.

Experimental Results

The TSP framework was evaluated on SWE-bench Lite, a benchmark consisting of 300 real-world GitHub issues. The results demonstrate that TSP significantly outperforms traditional Retrieval-Augmented Generation (RAG) methods.

Performance of TSP-Qwen2.5-Coder models on the SWE-bench Lite benchmark.

Key performance metrics include:

  • Superior Pass Rates: The TSP-Qwen2.5-Coder-14B model achieved an 8.33% pass rate, a substantial improvement over GPT-4 (1106) using standard RAG (2.67%).
  • Search Accuracy: TSP-trained models showed a dramatic increase in retrieval precision. The 14B model reached a search accuracy of 64.8%, compared to 28.7% for its base version.
  • Token Efficiency: By focusing on class and function-level snippets, the TSP framework consumed significantly fewer tokens than file-level RAG (3724.56 vs. 16933.77 tokens on average), making it both faster and more cost-effective.

Conclusion and Future Work

Think-Search-Patch represents a significant step forward in repository-level code repair by combining fine-grained retrieval with structured reasoning. While it currently follows a linear pipeline, the authors suggest that future iterations could incorporate "agentic" behaviors, such as dynamic decision-making and iterative search loops, to further bridge the gap between automated tools and human developers. Currently, TSP provides a robust, efficient, and highly accurate framework for maintaining complex software repositories.

Summary updated

BibTeX
@misc{xiong2025think,
  author = {Bojian Xiong and Yikun Lei and Xikai Liu and Shaowei Zhang and Pengcheng Zhu and Yan Liu and Yongqi Leng and Ling Shi and Meizhi Zhong and Yurong Zhang and Yan Gao and Yiwu and Yao Hu and Deyi Xiong},
  title = {{Think-Search-Patch: A Retrieval-Augmented Reasoning Framework for Repository-Level Code Repair}},
  year = {2025},
  doi = {10.18653/v1/2025.emnlp-industry.109},
}
Figure