CoSQA+: Pioneering the Multi-Choice Code Search Benchmark with Test-Driven Agents

Jing Gong, Yanghui Wu, Linxi Liang, Zibin Zheng, Yanlin Wang, Liu, Mingwei, Zheng, Zibin · DOI

Abstract

Semantic code search, retrieving code that matches a given natural language query, is an important task to improve productivity in software engineering. Existing code search datasets face limitations: they rely on human annotators who assess code primarily through semantic understanding rather than functional verification, leading to potential inaccuracies and scalability issues. Additionally, current evaluation metrics often overlook the multi-choice nature of code search. This paper introduces CoSQA+, pairing high-quality queries from CoSQA with multiple suitable codes. We develop an automated pipeline featuring multiple model-based candidate selections and the novel test-driven agent annotation system. Among a single Large Language Model (LLM) annotator and Python expert annotators (without test-based verification), agents leverage test-based verification and achieve the highest accuracy of 93.9%. Through extensive experiments, CoSQA+ has demonstrated superior quality over CoSQA. Models trained on CoSQA+ exhibit improved performance. We publicly release both CoSQA+_all, which contains 412,080 agent-annotated pairs, and CoSQA+_verified, which contains 1,000 human-verified pairs, at https://github.com/DeepSoftwareAnalytics/CoSQA_Plus.

In real-world software development, a single natural language query like "how to read a file in Python" rarely has just one "correct" implementation. There are often multiple valid snippets depending on the library used or the specific context. However, traditional benchmarks like CodeSearchNet or the original CoSQA are built on a one-to-one matching paradigm. This design forces the use of metrics like Mean Reciprocal Rank (MRR), which only rewards the model for finding the first relevant result.

Distribution of matched codes per query in CoSQA+, highlighting the multi-choice nature of real-world code search.

Furthermore, existing datasets rely on human "surface-level" understanding. Annotators read code and guess its functionality, which is both unscalable and prone to missing subtle logic errors that only manifest during execution. CoSQA+ addresses these gaps by shifting to a multi-choice paradigm and employing a rigorous, execution-based verification pipeline.

Methodology: Test-Driven Agent Annotation

The creation of CoSQA+ involves a sophisticated automated pipeline designed to ensure both scale and accuracy. The process is divided into three primary stages:

  1. Candidate Selection: To minimize model-specific bias, the authors used an ensemble of CodeBERT, UniXcoder, and CodeT5+ to retrieve the top 20 candidate snippets for each query from a pool of over 650,000 Python functions.
  2. Test-Driven Agent Annotation: This multi-agent system moves beyond simple LLM prompting. A Preliminary Screener triages pairs into "clear match," "clear non-match," or "unclear." For "unclear" cases, a Test Program Generator creates a full Python test suite. A Test Executor & Bug Fixer then runs the code in a sandbox, iteratively repairing the test environment if necessary. Finally, a Final Arbiter makes the labeling decision based on actual execution logs and assertion results.
The CoSQA+ pipeline: from query collection to multi-model candidate construction and test-driven agent annotation.

This approach resulted in two datasets: CoSQA+_all (412,080 agent-annotated pairs) and CoSQA+_verified (a gold-standard subset of 1,000 human-verified pairs).

Key Insights and Results

The most significant finding is that execution-based verification outperforms human intuition. Agents achieved a labeling accuracy of 93.9%, surpassing human experts (89.1%) who relied solely on code reading. The "Bug Fixer" agent proved essential; without it, many valid snippets were incorrectly rejected due to minor environment issues.

By embracing the multi-choice nature of code search, CoSQA+ enables the use of Mean Average Precision (MAP), providing a more comprehensive evaluation of a model's ability to retrieve all relevant solutions. Models fine-tuned on CoSQA+ consistently outperformed those trained on the original CoSQA across standard benchmarks. Additionally, the pipeline demonstrated strong cross-language generalization, improving accuracy for PHP, Java, and Go by an average of 10.33% over standalone LLM annotators.

Conclusion and Future Work

CoSQA+ sets a new standard for code search benchmarks by prioritizing functional verification over semantic guessing. While highly effective, the system still faces challenges with query ambiguity, where vague queries can lead to multiple valid but conflicting interpretations. Future research will focus on developing multi-interpretation test generation and query clarification mechanisms to further refine the "unclear" cases in automated annotation.

Summary updated

BibTeX
@article{gong2026cosqa,
  author = {Jing Gong and Yanghui Wu and Linxi Liang and Zibin Zheng and Yanlin Wang and Liu, Mingwei and Zheng, Zibin},
  title = {{CoSQA+: Pioneering the Multi-Choice Code Search Benchmark with Test-Driven Agents}},
  journal = {IEEE Trans. Software Eng.},
  year = {2026},
  doi = {10.1109/tse.2025.3631886},
}
Figure