TL;DR
Large Language Models (LLMs) are often "polite" only at the surface; if an attacker forces them to start a harmful sentence, they frequently lose their way and continue the unsafe trajectory. DeepRecover introduces a "safety anchor" that teaches models how to snap out of a harmful spiral and return to safe behavior, regardless of how far into a generation the attack occurs.
What Problem Does This Paper Address?
We’ve all seen the headlines: LLMs can be "jailbroken" or tricked into providing dangerous information. One particularly sneaky way to do this is through adversarial prefixes. Instead of just asking a bad question, an attacker forces the model to begin its response with a specific harmful phrase, like "Step 1: Gather the following chemicals..."
The problem is that safety alignment in current models is often "shallow." They are trained to refuse bad prompts at the very beginning, but once they’ve already started down a dark path (the prefix), they lack the "recovery" muscles to stop. Even worse, if you train a model to recover from a 10-token harmful start, it often fails completely if the harmful start is 50 tokens long. This is the cross-depth generalization gap: the model's ability to stay safe doesn't scale with the length of the conversation.
Methods
The authors propose DeepRecover, a framework designed to make safety "depth-invariant." It essentially gives the model a North Star to follow when it finds itself in a bad neighborhood.
- The Privileged Anchor: During training, they use a "Teacher" model that has access to "Privileged Safety Information"—basically, the teacher knows exactly why the query is bad and what a safe response looks like. This creates a "perfect" safe trajectory (the anchor).
- Transition Tokens: To bridge the gap between a "corrupted" state (the harmful prefix) and a "safe" state, the model is taught to use specific transition phrases like "Wait, I should reconsider" or "Actually, I cannot do this." These act as a steering wheel to turn the model back toward safety.
- Unbalanced Optimal Transport (UOT): This is the secret sauce. Standard training (like KL divergence) forces the "Student" model to match the "Teacher" token-for-token. But because the Student is starting from a corrupted state and the Teacher is starting from a clean one, their paths will naturally look different. UOT allows for a more flexible alignment. It treats the probability distributions of tokens like piles of sand and finds the most efficient way to move the Student's "mass" to match the Teacher's safe "shape," without demanding they be identical.
Datasets
The researchers put DeepRecover through its paces using a variety of benchmarks:
- Training: 4,000 harmful examples from the Gretel synthetic safety alignment dataset and 2,000 benign examples for balance.
- HEx-PHI: Used to evaluate recovery under controlled harmful prefixes of varying lengths.
- Do-Not-Answer & HarmBench: Standard safety benchmarks to ensure the safety generalizes across different types of risks.
- XSTest: To check for "over-refusal" (when a model gets too scared and refuses harmless questions).
- MMLU: To ensure the model doesn't lose its general intelligence while becoming safer.
Key Insights and Ideas
- Safety as a Capability, Not a State: The core "aha!" moment here is that safety shouldn't just be a list of forbidden words. It should be a recovery capability—the internal logic required to recognize a harmful trajectory and pivot away from it.
- Flexibility is Key: By using Unbalanced Optimal Transport, the authors acknowledge that a model recovering from an attack shouldn't sound exactly like a model that was never attacked. It just needs to reach the same safe destination.
- Breaking the Depth Barrier: Most safety methods are "brittle"; they only work at the specific depths they were trained on. DeepRecover shows that by anchoring to a depth-independent reference, the model learns a rule that applies even to much longer, unseen attack lengths.
Key Results
The results are quite striking. In tests across four different LLMs (Llama and Qwen families), DeepRecover maintained a low Attack Success Rate (ASR) even when the attacks were much deeper than anything seen during training.
- Generalization: On Llama-3.2-3B, while other methods saw their ASR jump to over 40% on "unseen" deep prefixes, DeepRecover kept it down to a mere 2.63%.
- Long-Context Resilience: Even in "stress tests" with prefixes up to 3,000 tokens long, DeepRecover remained significantly more robust than vanilla models or other state-of-the-art safety methods.
- Preserving Brains: Crucially, the models didn't get "dumber." Performance on the MMLU benchmark remained stable, meaning you don't have to sacrifice IQ for safety.
Limitations and Future Work
While DeepRecover is a major step forward, the authors note a few areas for improvement:
- The "Safety Tax": Like most safety methods, there is a slight increase in "over-refusal" (refusing benign queries that look vaguely suspicious).
- Fixed Vocabulary: The method currently relies on a predefined set of transition tokens (the "pivots"). Future work could explore letting the model learn its own, more natural ways to pivot.
- Inference Costs: While the "Teacher" is only used during training, the training process itself is more complex than standard fine-tuning due to the UOT calculations.
In short, DeepRecover suggests that the path to truly safe AI isn't just about building higher walls at the entrance, but teaching the AI how to find its way back home when it gets lost in the woods.
Summary updated