Tree-like self-play raises secure code pass rate to 75.8% on Python benchmarks
Tree-like Self-Play improves secure code generation pass rate to 75.8%, outperforming SFT's 57.0%
AI/ML arXiv 2 Jun 2026 Method Prototype evidence
Published as “Learn from Your Mistakes: Tree-like Self-Play for Secure Code LLMs”
Why it matters
TSP offers a scalable way to harden code-generation LLMs against a broad range of security flaws without extensive human annotation, potentially reducing vulnerable code in real-world software pipelines.
Summary
TSP raises CodeLlama-7B’s security pass rate (SPR@1) to 75.8%, well above SFT’s 57.0% and other baselines. The method builds a decision-tree self-play game that generates both secure and vulnerable code paths, giving dense on-policy feedback at vulnerable nodes. Experiments on Python (SecurityEval, 121 prompts) and C/C++ (DiverseVul, 150 unseen CWE test samples) show consistent security gains and cross-language transfer, though TSP still struggles with complex memory-flow bugs and sometimes yields higher overall vulnerability counts than SFT. Evidence is based on public benchmarks and comparisons to Base LLM, SFT, SafeCoder, and a non-tree self-play baseline.
What they found 1
- TSP boosts CodeLlama-7B’s security pass rate (SPR@1) to 75.8%, outperforming SFT (57.0%).
“TSP boosts CodeLlama-7B’s pass rate (SPR@1) to 75.8%, significantly outperforming SFT (57.0%)”
Caveats 5
- TSP sometimes yields higher overall vulnerability counts than SFT, e.g., on CodeLlama-7B the vulnerability count is 35.2 for TSP versus 32.3 for SFT.
- However, TSP underperforms on complex memory and implicit data-flow vulnerabilities, such as CWE-690, CWE-125, and CWE-416, where the unsafe decision and the eventual manifestation are separated by long execution distances with seemingly legitimate intermediate steps, causing the value estimator to misjudge intermediate safety.
- TSP excels at vulnerabilities with local, explicit control flows-such as CWE-215, CWE-079, and CWE-252-where the security-critical decision and its manifestation are co-located, allowing the value network to prune dangerous branches early.
- The current risk node abstraction operates through a primarily token-level lens and cannot fully capture multi-line data-flow dependencies and cross-variable invariants.
- Self-play negative samples become less challenging as the model improves, potentially limiting discovery of deeper vulnerability patterns.
What’s new here 4
- Introduces a tree-structured self-play framework that creates on-policy positive and negative examples at identified “risk nodes”.
- Provides fine-grained, token-level feedback rather than sequence-level signals used in standard SFT or RL approaches.
- Demonstrates cross-language security generalization from C/C++ training to Python, Go, JavaScript, and Ruby without additional data.
- Shows improved security on unseen CWE categories, indicating learning of abstract security principles.