Learn from Your Mistakes: Tree-like Self-Play for Secure Code LLMs
Wenqi ChenZiyan ZhangBin WangLin LiuHengheng ZhangZhengsu Chen
Wenqi Chen
Ziyan Zhang
Bin Wang
Lin Liu
Hengheng Zhang
Zhengsu Chen
+6
Tree-like Self-Play (TSP) reframes secure code generation as a fine-grained sequential decision process. Unlike standard sequence-level alignment methods, TSP builds a decision tree where the model explores both secure and vulnerable branches, then learns to discriminate against its own localized errors at critical decision nodes. This dense, on-policy signal forces self-correction where vulnerabilities emerge. On Python security benchmarks, TSP boosts CodeLlama-7B's pass rate (SPR@1) to 75.8%, significantly outperforming SFT (57.0%) and unstructured self-play. Crucially, TSP reduces vulnerabilities in unseen CWE categories by 24.5% and transfers security principles from C/C++ to Python, Go, and JavaScript, indicating internalization of abstract, language-agnostic security logic.

arXiv ·
· June 2, 2026