mlsanityData to 7 Oct 2026

Learn from Your Mistakes: Tree-like Self-Play for Secure Code LLMs

arXiv 2606.03489 ↗First posted 2 Jun 2026cs.CR cs.AIcited by 0

On the map: Synthetic data and self-improvement for LLMs → Code and instruction data synthesis

Abstract (arXiv)

While Large Language Models (LLMs) excel in code generation, they remain prone to replicating subtle yet critical vulnerabilities endemic to their training data. Current alignment techniques, such as Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL), typically apply coarse-grained optimization at the sequence level. This approach often fails to address the localized nature of security flaws, where a single incorrect token choice can compromise an entire program. To bridge this gap, we introduce Tree-like Self-Play (TSP), a framework that reframes secure code generation as a fine-grained sequential decision process. Unlike standard methods that blindly maximize likelihood, TSP constructs a decision tree where the model explores branching trajectories--generating both secure "golden paths" and vulnerable variants. By treating code generation as a self-play game, the model learns to strictly discriminate against its own localized errors. This provides a dense, on-policy learning signal that forces self-correction precisely at the critical decision nodes where vulnerabilities typically emerge. Our experiments demonstrate that TSP fundamentally enhances model reliability. In Python security benchmarks, TSP boosts CodeLlama-7B's pass rate (SPR@1) to 75.8%, significantly outperforming SFT (57.0%) and unstructured self-play baselines. Crucially, TSP induces robust out-of-distribution generalization: the model not only reduces vulnerabilities in unseen categories (CWEs) by 24.5% but also successfully transfers security principles learned from C/C++ to diverse languages, including Python, Go, and JavaScript. This suggests that TSP does not merely memorize patches, but internalizes abstract, language-agnostic security logic.

Key facts. No citing papers in the map yet (data through 7 Oct 2026). Placed in Code and instruction data synthesis, one of 13 directions in Synthetic data and self-improvement for LLMs. Builds on 37 of its 47 references that are in the map. First posted on arXiv 2 Jun 2026.

Builds on

37 of 47 references.

References · 37

  1. 2022Training language models to follow instructions with human feedbackcited by 15,712
  2. 2023Direct Preference Optimization: Your Language Model is Secretly a Reward Modelcited by 7,916
  3. 2017Deep reinforcement learning from human preferencescited by 3,846
  4. 2022Scaling Instruction-Finetuned Language Modelscited by 2,997
  5. 2022Constitutional AI: Harmlessness from AI Feedbackcited by 2,950
  6. 2022Competition-Level Code Generation with AlphaCodecited by 1,427
  7. 2019Dota 2 with Large Scale Deep Reinforcement Learningcited by 1,084
  8. 2023WizardCoder: Empowering Code Large Language Models with Evol-Instructcited by 728
  9. 2024Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Modelscited by 552
  10. 2021Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributionscited by 417
  11. 2022CodeRL: Mastering Code Generation through Pretrained Models and Deep Reinforcement Learningcited by 342
  12. 2023Preference Ranking Optimization for Human Alignmentcited by 283
25 more
  1. 2023Magicoder: Empowering Code Generation with OSS-Instructcited by 251
  2. 2022Do Users Write More Insecure Code with AI Assistants?cited by 186
  3. 2024Preference Fine-Tuning of LLMs Should Leverage Suboptimal, On-Policy Datacited by 184
  4. 2024Direct Nash Optimization: Teaching Language Models to Self-Improve with General Preferencescited by 176
  5. 2023Large Language Models for Code: Security Hardening and Adversarial Testingcited by 149
  6. 2024RLEF: Grounding Code LLMs in Execution Feedback with Reinforcement Learningcited by 146
  7. 2024A Minimaximalist Approach to Reinforcement Learning from Human Feedbackcited by 141
  8. 2024Token-level Direct Preference Optimizationcited by 135
  9. 2024Understanding the performance gap between online and offline alignment algorithmscited by 110
  10. 2025TreeRL: LLM Reinforcement Learning with On-Policy Tree Searchcited by 75
  11. 2024Instruction Tuning for Secure Code Generationcited by 63
  12. 2023Data Quality for Software Vulnerability Datasetscited by 49
  13. 2024Align Anything: Training All-Modality Models to Follow Instructions with Language Feedbackcited by 38
  14. 2024AutoSafeCoder: A Multi-Agent Framework for Securing LLM Code Generation through Static Analysis and Fuzz Testingcited by 34
  15. 2024Getting More Juice Out of the SFT Data: Reward Learning from Human Demonstration Improves SFT for LLM Alignmentcited by 32
  16. 2024CodeDPO: Aligning Code Models with Self Generated and Verified Source Codecited by 32
  17. 2024ProSec: Fortifying Code LLMs with Proactive Security Alignmentcited by 24
  18. 2022VUDENC: Vulnerability Detection with Deep Learning on a Natural Codebase for Pythoncited by 17
  19. 2024HexaCoder: Secure Code Generation via Oracle-Guided Synthetic Training Datacited by 14
  20. 2024How to Leverage Demonstration Data in Alignment for Large Language Model? A Self-Imitation Learning Perspectivecited by 14
  21. 2025A.S.E: A Repository-Level Benchmark for Evaluating Security in AI-Generated Codecited by 12
  22. 2025VulnLLM-R: Specialized Reasoning LLM with Agent Scaffold for Vulnerability Detectioncited by 11
  23. 2024Reward Generalization in RLHF: A Topological Perspectivecited by 9
  24. 2025RefleXGen:The unexamined code is not worth usingcited by 4
  25. 2025Is Your Prompt Poisoning Code? Defect Induction Rates and Security Mitigation Strategiescited by 3

Built on by

Not cited yet.

Similar, not cited

Close in wording, no citation either way

  1. 2026Secure Code Generation via Online Reinforcement Learning with Vulnerability Reward Model
  2. 2026Anchored Self-Play for Code Repair
  3. 2026SoK: AI Secure Code Generation: Progress, Pitfalls, and Paths Forward
  4. 2026Distilling Game Code World Model Generation into Lightweight Large Language Models
  5. 2025Teaching an Old LLM Secure Coding: Localized Preference Optimization on Distilled Preferences
  6. 2026Enhancing Reliability in LLM-Based Secure Code Generation
  7. 2025Fortifying LLM-Based Code Generation with Graph-Based Reasoning on Secure Coding Practices
  8. 2025Chasing Moving Targets with Online Self-Play Reinforcement Learning for Safer Language Models