arXiv:2609.03955cs.CLSep 2026

Manuscript Offprint

arXiv:2609.03955

Two-Stage Reinforcement Learning for Sound and Adversarial Test Generation in Code LLMs

Jiacheng Xu, Wentao Zhang, Zhiyi Lyu, Fuxiang Zhang, Chaojie Wang, Yang Liu, Bo An

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Reinforcement learning (RL) has substantially advanced code generation with large language models (LLMs) through executable feedback. The feedback for coding problems mainly comes from specific test cases, where high-quality test cases are often scarce since they should be both sound and discriminative. We thus turn to study the auto-generation of test cases using the learned model. We find this is naturally an adversarial RL problem: the model is expected to generate effective test cases as counterexamples, depending on the solver's current failure modes. We propose Test Cases Scaling (TCS), a two-stage RL framework for effective test generation. Both stages train a test generator from a rolling policy-aligned buffer: Stage 1 generates tests consistent with the reference solution, and Stage 2 restricts the buffer to current failure modes and learns counterexample tests. Across TACO and LiveCodeBench, TCS improves both pass@1 and inference-time answer selection according to generated tests. We find the learned test generator also enables effective selection among other LLM outputs.

1 Introduction

Code generation is one of the most consequential applications of large language models (LLMs), improving developer productivity and lowering barriers for non-experts (Dong et al., 2025). Modern code LLMs have moved far beyond heuristic systems (Gulwani, 2010; Roziere et al., 2023; Hui et al., 2024; Dong et al., 2025), and reinforcement learning (RL) has become a particularly effective post-training tool because candidate programs can be verified by execution on test cases, often outperforming purely supervised tuning based on costly demonstrations (Shojaee et al., 2023; Le et al., 2022; Luo et al., 2025).

As code models improve, the bottleneck shifts from producing plausible solutions to verifying which candidate is actually correct. This makes

Figure

Figure 1: TCS enables adversarial test case generation using ground-truth solutions.

inference-time self-verification attractive: generate tests, execute candidate programs, and select the program with the strongest empirical support. For code tasks, this is appealing because it relies on executable evidence rather than a generic scalar reward. But test-based selection helps only when generated tests are reliable and targeted. Unsound tests can mis-rank candidates, while weak tests fail to expose subtle bugs. In our experiments, self-generated tests help substantially, yet curated public tests can still outperform them when used alone, showing that the challenge is not to generate more tests, but better ones.

This challenge explains why RL is useful here beyond supervised imitation. Test generation is a multi-solution, utility-driven objective: many tests can be correct, as judged by execution rather than exact target matching. The objective is non-stationary because informative tests depend on the solver's current failure modes. Whereas code generation can optimize against a fixed test suite, test generation must produce tests that are sound for a reference solution and discriminative against plau-sible incorrect code. Optimizing only for soundness yields trivial tests, while counterexample rewards are sparse and unstable from scratch, making reward design particularly important (Chen et al., 2024; Fu et al., 2025).

We therefore study an execution-verifiable post-training setting in which a ground-truth solution is available to validate generated tests. This lets us separate two objectives that are easy to conflate: soundness control through ground-truth verification and candidate-conditioned adversariality against the model's evolving failure modes. This setting provides a clean test bed for understanding what makes learned test generation useful. Based on this view, we propose Test Cases Scaling (TCS), a two-stage RL framework that jointly trains code generation and test generation with stage-specific rewards and a rolling policy-aligned buffer. Stage 1 learns tests that agree with the ground-truth solution, while Stage 2 learns candidate-conditioned counterexample tests that pass the reference solution but fail plausible incorrect programs. At inference time, the learned verifier generates tests for candidate programs, and we select the candidate with the highest pass-count across pooled tests.

Experiments on TACO and LiveCodeBench show consistent gains in both training-time and inference-time performance. The paper makes three main contributions:

  • We argue that effective test generation for code LLMs requires both soundness control and candidate-conditioned adversariality, and formulate this view in an execution-verifiable post-training setting where generated tests can be checked against a ground-truth solution.

  • We propose Test Cases Scaling (TCS), a two-stage RL framework that instantiated these principles: Stage 1 learns ground-truth-verified tests, and Stage 2 learns candidate-conditioned counterexample tests via stage-specific rewards and a policy-aligned buffer.

  • We provide theoretical and empirical evidence for test-based inference-time scaling. We derive an exponential reliability bound for pass-count selection under self-generated tests (Sec. 3.4), and experiments on TACO and LiveCodeBench show improvements over joint SFT, code-only RL, and test-only RL.

The learned verifier can also improve the selection of outputs from strong external LLMs.