TL;DR
Giving an AI agent direct shell access is thrilling until it quietly compounds a few innocent commands into deleting your /home directory. Instead of relying on external rule-checkers, this paper introduces Agent MechSuits, a framework that reads an agent's internal "brainwaves" (hidden states) at each step of a task to catch emerging risks on the fly. Even cooler, by nudging just 10 numbers inside a single neural network layer, it steers the agent away from executing malicious bash commands without altering the model's weights.
What Problem Does This Paper Address?
Command-Line Interface (CLI) agents—tools like Claude Code or Gemini CLI—don't just chat; they execute commands, read terminal outputs, and adapt over dozens of steps. In a standard single-turn Q&A, it’s relatively easy to spot a toxic prompt. In a multi-turn CLI trajectory, risk is sneaky and cumulative. Creating an empty script is harmless; changing its permissions is routine; writing a destructive payload into it five steps later is catastrophic.
Existing agent defenses mostly act like external bouncers—guardrails watching text inputs and outputs against predefined rules, which struggle to steer nuanced, evolving behaviors. Meanwhile, mechanistic interpretability (peeking inside a model's activations to spot and steer unsafe intent) has largely been stuck in static, single-turn Q&A settings. The authors bridge this gap, asking: Can we monitor and steer a multi-turn CLI agent from the inside out, step by step, while it works?
Methods
Agent MechSuits is an inference-time, white-box defense operating in two stages without modifying model weights:
- Stage 1: Runtime Safety Detection. Using standard linear probing, the authors train a lightweight logistic classifier on the model's hidden state at a specific intermediate layer. What's new is applying this dynamically across multi-turn trajectories: at every step, before emitting a command, the probe inspects the last-token representation of the entire accumulated history (past commands + terminal feedback) to flag whether the current state is risky.
- Stage 2: Sparse Subspace Steering. When risk is flagged, the framework intervenes at that exact same layer. Rather than shifting all hidden dimensions, it looks at the trained probe's weights to find the Top-10 dimensions that best separate "benign" from "harmful." It builds a binary steering vector on just those 10 dimensions and broadcasts it across all token positions, scaled by a strength parameter $\alpha$. An external LLM judge (DeepSeek-V4-Pro) adaptively searches for the sweet-spot $\alpha$ that maximizes safety while preserving task utility.
Datasets
Existing agent safety benchmarks mostly check whether a final task succeeded or violated a policy. To study internal step-by-step dynamics, the authors built the Mechanistic Agent Safety (MAS) benchmark:
- 194 tasks (98 malicious OS-level attacks mapped to MITRE ATT&CK, 96 benign), drawn from AdvCUA, Safe-OS, and AgentBench.
- Step-annotated trajectories generated by three open-weight ReAct agents (LLaMA-3.1-8B, Qwen-2.5-7B, and Gemma-2-9B) running inside real Docker containers.
- Counterfactual augmentation: Benign steps crafted to mimic the exact syntax of malicious bash commands, ensuring probes learn genuine harmful intent rather than memorizing scary-looking syntax.
The authors also use Safe-OS for an out-of-distribution prompt-injection stress test.
Key Insights and Ideas
- Safety signals peak mid-network: Across all three models, linear probes hit peak accuracy in early-to-middle layers (around layers 12–17) rather than at the final output layers—meaning the model "knows" an action is unsafe well before formatting the bash command.
- Agents telegraph future mistakes: Without retraining, the exact same current-step probe can often spot danger one step before a critical malicious command is even generated (lookahead detection), outperforming heavy LLM-as-a-judge baselines.
- 10 dimensions hold causal power: To prove the 10-dimensional subspace isn't just picking up superficial correlations, the authors flipped the steering vector backwards on steps where agents naturally refused malicious requests. Reversing those 10 dimensions overrode the agents' safety training, turning polite refusals into active
rm -rfattacks (100% attack success on LLaMA-3.1-8B). - The detection–control gap: Under indirect prompt injection (malicious instructions hidden inside environment output), the probe struggled to detect the threat. Yet manually triggering the steering vector still neutralized the attack—showing internal control generalizes further than internal detection.
Key Results
- Detection: Agent MechSuits averages 97.32% accuracy on current-step risk detection (99.15% on LLaMA-3.1-8B, 97.46% on Qwen-2.5-7B, 95.35% on Gemma-2-9B), beating frontier LLM judges (including GPT-4o, Claude-4.6, and Gemini-3-Pro) and external guardrails like LLaMA-Guard-4-12B and AGrail. On lookahead detection, it reaches 92.31% (LLaMA) and 96.77% (Qwen).
- Mitigation: Steering slashes the average Attack Success Rate (ASR) by 72.63 percentage points—dropping ASR from 85.99% down to 13.36% across models (e.g., from 91.30% to 4.35% on Qwen-2.5-7B), outperforming RepE and prompt-based Self-Reminder baselines.
- Prompt Injection Stress Test: When manually activated on Qwen-2.5-7B trajectories hijacked by environmental prompt injections, steering drops ASR from 86.7% to 6.7% with 0% generation collapse.
Limitations and Future Work
- Generation collapse: Pushing internal activations too hard can break the model's fluency, causing repetition loops or truncated text. Agent MechSuits raises the average generation collapse rate to 18.54% (up from 2.24% vanilla), highlighting a clear trade-off between strict safety and clean outputs.
- White-box requirement: You need direct access to hidden states and activations, ruling out closed-API frontier models.
- Sparse lookahead data & OOD detection: Current open-weight models struggle with long-horizon CLI tasks, leaving relatively few natural lookahead examples to test on, and the linear probe alone fails to reliably catch indirect prompt injections. Future work aims to build smoother, utility-preserving steering controllers and dig deeper into how trajectory-level risk builds up over time.
Summary updated