A training defense helps code models keep refusing harmful requests
Trained on Harm, Still Refusing: A Training-Time Defense for Code Generation · arXiv:2611.27495
In plain words
This paper introduces SAFEHOLD, a provider-run way to train a code model further without dropping its safety behavior. Before training, the provider saves how the aligned model responds to a few harmful prompts.
During training, SAFEHOLD keeps the first response choice close to that saved version and practices every part of a refusal.
Across five tested code models, it reduced judged harmful answers compared with training without the defense. The evaluation does not cover every model or kind of training data.
The research
The problem matters when a safety-aligned code model is adapted on harmful examples: a refusal can fail after it begins, even while harmfulness remains recognizable.
In the tested setup, SAFEHOLD changes the expectation that stronger protection must mean more full refusals to safe prompts, since that rate was lower than with undefended training.
SAFEHOLD is a provider-controlled training defense that preserves a code model’s first-response preferences and trains it to continue a complete refusal.
Researchers trained five code-capable models for 600 updates on harmful examples, comparing SAFEHOLD with undefended training.
Across three tests of harmful code generation, average attack success rate—the share of requests judged at least moderately harmful—fell from 95.08% to 23.05%; unusable replies counted as unsuccessful attacks.
On safe prompts, full refusals among valid responses averaged 2.73% with SAFEHOLD and 58.95% without it. Together, the results support lower harmful compliance without more full refusals to safe requests in this evaluation.
The evaluation covered five open-weight, code-capable models and relied mainly on one general-domain harmful training corpus. Larger and closed-source models, more model families, and more varied training data were not tested.
What the evidence shows
- The main comparison
SAFEHOLD is a provider-controlled training defense for safety-aligned code models. Across five tested models and three tests of harmful code generation, average attack success rate (the share of requests judged at least moderately harmful) was 23.05% with SAFEHOLD, compared with 95.08% under undefended training. This average covers 15 model-test combinations; individual results vary.
- Recognition can outlast refusal
The safety failure was not simply a loss of information about harmful requests. Separately trained probes distinguished harmful from benign requests in late-layer representations after harmful training, even as models often complied with harmful requests.
The probe result measures how separable those requests remained; it does not show that the model’s original safety mechanism was preserved.
- Training domain was not a safeguard
The problem also crossed training domains in one tested model. For Qwen2.5-Coder-7B-Instruct, general-domain harmful training produced higher harmful-code test scores than either of two code-focused training sets. So, in this comparison, code safety could be weakened without using code-specific harmful examples.
- Safe requests remained answerable
The harmful-response reduction did not coincide with more full refusals to safe requests. Across five models, full refusals among valid responses to safe XSTest prompts averaged 2.73% with SAFEHOLD, compared with 58.95% under undefended training.
The measure concerns responses to safe prompts, so it complements rather than replaces the harmful-request result.
- The full refusal sequence mattered
A separate comparison supports using both parts of SAFEHOLD in the tested setup. Across five models and three harmful-code tests, the full combination averaged 23.05% ASR, versus 61.68% when anchoring was paired with supervision of only the first refusal token. The result suggests that continuing the refusal matters beyond preserving its opening.
Deep
- Original paper authors
- Miaoqing Tian, Ruohan Li, Qian Lu, Haipeng Qu
- Submitted to arXiv
- License
- arXiv non-exclusive