Editor’s introduction
The study evaluates CoSPlay, a test-time scaling method that jointly refines code and unit tests through cooperative self-play. It explores solution strategies and failure modes to generate discriminative unit tests, then uses execution results to iteratively improve both code and test quality. The framework cycles through code and test refinement using pass-count signals from execution, allowing both pools to co-evolve. When multiple codes tie in performance, it selects the one with the highest output consistency across inputs. Results show significant gains in both code correctness and unit test accuracy, outperforming baseline methods without ground-truth tests.
Introduction
While TTS methods avoid weight updates, they still need to evaluate the code quality at inference time for refinement, filtering, and selection. When GT UTs are available, these decisions can be grounded in trusted input-output checks. In GT-free settings, however, it must be constructed from self-generated UTs, typically by sampling codes and tests before heuristic or mathematical re-ranking; we provide a broader discussion in Appendix C. Yet scaling this pipeline is fragile: generated UTs may have incorrect expected outputs, fail to distinguish among plausible candidates, or spuriously agree with wrong code. The key question therefore shifts from merely sampling more code or UTs to a co-evolution perspective: Can codes and UTs co-evolve at inference time to achieve competitive code generation without GT?
To this end, we present CoSPlay, a GT-free, training-free three-stage pipeline that creates a co-evolving loop between code candidates and UTs. First, Exploration-Attack-Guided Code-UT Idea Generation bootstraps the loop by exploring plausible solution strategies and deriving targeted UT ideas from their potential failure modes, so the initial UT pool is grounded in solution-specific assumptions and can better distinguish plausible code candidates. Given this initialized pool, Execution-Matrix-Driven Iterative Self-Play lets codes and UTs evaluate each other: high-support UTs provide reliable refinement signals, while high-support codes provide evidence for judging UTs. These pass-count signals guide code-side pruning/refinement and UT-side refreshing/replacement, progressively improving both pools. Finally, when the evolved Code-UT pool still leaves BoN-tied codes, Output-Consensus-Based Cluster Selection handles the remaining ambiguity among BoN-tied codes by comparing their outputs on random valid inputs and selecting the most reliable consensus cluster and code, since correct codes should agree while incorrect codes tend to diverge.
Output-Consensus-Based Cluster Selection
Theoretical Motivation. Correct code candidates should share the same output signature, while incorrect code candidates typically split across different behaviors. As the number of random inputs increases, matching any fixed wrong signature becomes exponentially harder, whereas all correct candidates keep sharing the true signature. Thus, the true signature becomes the dominant population mode, and the empirical largest-cluster rule converges to it as the independently sampled candidate pool grows. Proposition A.3 formalizes this statement, with the proof given in Appendix A.
Conclusion and Future Directions
We introduce CoSPlay, a GT-free and training-free test-time scaling framework that jointly optimizes code candidates and unit tests via execution-driven co-evolution. CoSPlay first explores diverse high-level solution strategies together with their potential failure modes to derive discriminative UT ideas for better UT generation. It then leverages execution-matrix pass-count signals to iteratively refine both the code pool and the UT pool through cooperative self-play. Finally, for BoN-tied codes, CoSPlay further performs output-consensus clustering on random valid inputs. Since correct codes tend to produce consistent outputs while incorrect ones often diverge in different ways, it first selects the most reliable consensus cluster and then chooses the highest-reliability code within that cluster as the final solution. It consistently outperforms GT-free TTS baselines and matches or surpasses RLVR methods, improving both UT and code quality. These results suggest a scalable route toward GT-free test-time improvement for verifiable reasoning.
Limitation and Future Directions. CoSPlay relies on an executable environment to obtain Code-UT feedback, making direct extension to non-executable reasoning tasks more challenging. CoSPlay also consumes more tokens due to iterative Code-UT evolution. However, under comparable test-time compute budgets, it achieves the best performance among evaluated methods and substantially outperforms existing GT-free TTS methods. Improving token efficiency and extending to broader reasoning tasks remain promising directions.
Analysis of Pass-Count Signals
This section analyzes an i.i.d. model for the standard BoN baseline where codes and UTs are sampled independently. The goal is to explain why pass counts can serve as statistical signals for both code quality and UT quality. The first part introduces the execution-matrix setup and the thresholds under which pass counts allow codes and UTs to judge each other’s reliability. The second part derives the posterior direction and convergence rate induced by binomial support counts.
TTS for LLM Code Generation
Test-Time Scaling (TTS) strategies typically begin with repeated sampling, such as Best-of-N (BoN), but they often struggle to identify the best candidate without GT data. To improve selection, consensus-based methods such as CodeT and MPSC use self-generated UTs for voting, although CodeT is limited by the potential unreliability of generated tests and MPSC incurs substantial overhead from extensive sampling. More broadly, sampling-based inference-time methods such as PowerSampling improve reasoning by drawing from sharpened sequence-level distributions without additional training, suggesting that stronger performance can sometimes be elicited purely through better test-time sampling. Beyond simple resampling, a growing line of work treats code generation as a structured search problem. SFS formulates code generation as black-box optimization in code space and improves exploration diversity to avoid local optima, while CodeTree organizes planning, generation, and debugging within a unified tree structure guided by both execution feedback and agent feedback. ThinkCoder further decomposes test-time computation into a thorough exploration stage followed by an optimal refinement stage, improving efficiency by first broadening the solution space and then sharpening promising candidates. Finally, execution-guided approaches leverage runtime feedback for iterative improvement. Methods such as ORPS, S*, LDB, and Reflexion use execution signals for process supervision or refinement. Another paper further shows that self-generated tests can support iterative debugging, while also highlighting the bias and reliability issues that arise when such tests are used naively.
Self-Play in LLM
The self-play paradigm, popularized by AlphaZero and AlphaGo, has recently been adapted to optimize LLMs via RL. However, most existing approaches in the coding domain remain dependent on external supervision and heavy training. For instance, CURE relies on GT UT to guide the co-evolution of generation capability of code and UT, while UTRL necessitates ground-truth code for improving model performance. Although Absolute-Zero achieves a fully self-supervised setup without human data, it yields limited performance gains despite the prohibitive training costs of RLVR training. Crucially, these methods are exclusively training-time optimizations. In contrast, CoSPlay explores inference-time self-play for GT-free code generation, leveraging the pass count in execution metrix as signals to iteratively refine and filter candidates without parameter updates.
Search in Natural Language Space
To enhance the reasoning capabilities of LLMs, recent studies have proposed Chain-of-Thought strategies into structured search processes within the natural language space. Frameworks such as Tree of Thoughts and Reasoning via Planning treat reasoning steps as a search tree, exploring various intermediate thoughts to reach a solution. In the specific domain of code generation, methods like PlanSearch demonstrate that generating high-level natural language plans before implementation significantly outperforms direct code generation. The primary benefit of such natural language search lies in its ability to introduce structural diversity, preventing the model from converging to a narrow set of repetitive solutions—a common issue in direct code resampling. However, most existing methods for code generation remain static, generating plans without leveraging dynamic execution feedback or iterative self-play to refine.
Ablation Variant Descriptions
We evaluate the contribution of each component through the following ablation variants. w/o exp-atk removes Stage 1 entirely, generating codes and UTs directly without solution-aware idea exploration. w/o self-play removes Stage 2 entirely, skipping all iterative refinement and selecting directly from the initial pool. w/o random UT initializes the UT pool using only failure-oriented inputs, removing the random-valid-input half of the hybrid initialization strategy. w/o non-trivial best replaces the targeted non-trivial Best UT selection in Step3 with a randomly chosen UT for code fixing. w/o self-consis. disables self-consistency during UT output validation, using a single-sample output as the expected value instead of majority voting. w/o self-play step2 removes Step 2 (breaking spurious Code-UT coupling) while retaining all other self-play steps. w/o self-play step3 removes Step 3 (code fixing) while retaining all other self-play steps. w/o resample-UT 0% disables replacement of zero-pass-rate UTs in Step 4, retaining UTs that all candidates fail. w/o resample-UT 100% disables replacement of full-pass-rate UTs in Step 4, retaining UTs that all candidates pass. w/o resample-Code 0% disables code resampling in Step 1, retaining all-failing code candidates instead of replacing them. w/o UT-iteration removes all UT-side update steps within self-play (Steps 2 and 4), keeping the UT pool fixed throughout all rounds while still performing code cleaning and fixing. w/o self-play & self-consis. removes both Stage 2 and self-consistency, serving as a lower-bound baseline. Baseline (Direct resample) applies no CoSPlay mechanisms, directly sampling a large pool of codes and selecting via BoN with self-generated UTs.
U.1.2 CoSPlay-14B
Example 1: Balance Scale Comparison. The buggy code attempts to solve a simple balance-scale problem, where the correct decision should be based on comparing the total weights on the two sides, i.e., A + B versus C + D. However, the implementation mistakenly compares the sums of reciprocals, 1/A + 1/B and 1/C + 1/D, which can lead to an incorrect ordering for many inputs. The generated UT directly targets this semantic error with A = 10, B = 10, C = 1, and D = 1. The expected output is Left, since the left side has total weight 20 while the right side has total weight 2. Nevertheless, the buggy code outputs Right, because its reciprocal-based comparison gives 0.2 on the left and 2 on the right. The refinement guidance correctly localizes this erroneous comparison and replaces the reciprocal sums with direct weight sums, producing a semantically correct fixed code.
Example 2: Two-Row Monkey Seating. The buggy code correctly caps the row-preference groups by their row capacities, but then miscomputes the remaining capacity as m - (a + b), effectively treating the classroom as if it had only one row. The generated UT exposes this bug with m = 10, a = 4, b = 4, and c = 11, where the expected answer is 19 because there are 20 total seats and all 11 no-preference monkeys can still be seated after placing the 4 row-1 and 4 row-2 monkeys. However, the buggy code outputs 10, since it incorrectly believes only 2 seats remain. The refinement guidance fixes the capacity calculation by replacing m - (a + b) with 2 * m - (a + b), producing a semantically correct fixed code.
Summary. Both examples show that Step 3 can provide precise refinement signals when the selected UT exposes a concrete semantic mismatch between the buggy implementation and the problem requirement. In the balance-scale example, the UT reveals that the code compares reciprocal values instead of total weights; in the monkey-seating example, the UT reveals that the code computes remaining capacity using only one row instead of two. In both cases, the failure is not caused by formatting or invalid inputs, but by a targeted test that activates the exact erroneous logic. This allows the LLM to perform a localized correction—replacing the wrong comparison formula in the first case and the wrong capacity formula in the second—rather than rewriting the entire solution from scratch, demonstrating the effectiveness of Step 3’s targeted refinement strategy.
U.2.3 Analysis for case study of UT-code coupling
In the 7B case, the before panel shows a clean Code-UT coupling pattern: the highlighted wrong code passes the highlighted low-pass UT, creating a false positive that can inflate the pass count of an wrong solution. After regeneration, the corresponding orange UT column no longer accepts the same wrong code, thereby removing this spurious agreement.
In the 14B case, the highlighted second-low UT also accepts the highlighted wrong/BoN-selected code before resampling, indicating that this UT can still provide misleading support for an wrong candidate. Unlike the 7B case, this column is not a unique-pass column; rather, it is a low-pass suspicious UT whose agreement with the highlighted wrong code contributes to the coupling phenomenon. After resampling, the highlighted wrong code fails the new UT, showing that the accidental agreement between the code and the previous UT has been broken.
Overall, the execution matrices show that Step 2 operates in a targeted manner: it replaces a suspicious low-but-nonzero-pass-rate UT column, while leaving the rest of the UT pool unchanged. This intervention removes the highlighted false positive and restores a more useful discriminative signal for subsequent refinement and selection.
Example 1: Rectangle Overlap Counting. The problem asks to count the maximum number of overlapping axis-aligned rectangles over any region. The shown candidate code is semantically flawed because it reduces the two-dimensional overlap problem to a one-dimensional event-style count and fails to correctly model overlaps along both axes. Nevertheless, it passes the initial False Pass UT because the UT is spuriously aligned with the same error. Specifically, the expected answer of the initial UT is 3, while the true maximum overlap is 4. The wrong code also outputs 3, which leads to a false positive. CoSPlay then resamples the suspicious UT using the spuriously passed code as a negative reference. The regenerated UT is still not oracle-correct, since its expected output remains imperfect. However, it successfully breaks the previous code and UT coupling because the same buggy implementation no longer agrees with the regenerated UT and is therefore filtered out. This example shows that the resampling step is not meant to guarantee an immediately perfect UT. Instead, it serves as a decoupling mechanism that disrupts accidental agreement between wrong code and wrong tests and recovers useful discriminative signal for selection.
These cases demonstrate that self-generated UTs can become misleading not only because their inputs are insufficiently diverse, but also because their expected outputs may accidentally agree with a specific erroneous implementation. Step 2 addresses this issue by identifying suspicious low-but-nonzero-pass-rate UTs and regenerating them with the incorrectly passing code as a negative reference. This targeted regeneration discourages repeated agreement with the same flawed logic, breaks spurious Code-UT coupling, and helps recover useful discriminative signal for refinement and selection without relying on ground-truth supervision.


