Repository navigation
Add tool-call risk analyzer for agent steps - #41
Merged
Merged
Conversation
Scores an agent step before the model acts. A logistic probe reads the last-token hidden state captured by probe_hidden_states during prefill and returns P(unsafe tool call) for the next action, so a caller can hold or reroute the step before any output token is sampled. Follows the H-Node layout. The repo carries only the inference side (numpy scorer, analyzer, infer config, demo); probe training lives in latent-state-auditing and the artifact will be hosted there. The scorer reproduces the training-side logreg_score on 480 stored Qwen2.5-7B-Instruct activations to 1.4e-6. tests/plugin covers scoring, the analyzer's layer lookup on both the RPC and disk formats, threshold override and the error paths, CPU only. Signed-off-by: Rishabh Sinha <[email protected]>
The probe is fit on AgentDojo v1 agent steps rendered with the Qwen2.5 tool-calling chat template and captured through vLLM-Hook itself, so it reads the same features the analyzer sees in serving. Layer 28 with a PCA-32 front end scores 0.869 AUROC on held-out user tasks; the infer config now captures layer 28. The demo downloads the probe and three AgentDojo steps from the training repo on first run (the H-Node pattern) instead of scoring a toy prompt the probe was never trained on. The use-case doc gains the artifact table, capture parity and serving cost numbers, and limitations. Signed-off-by: Rishabh Sinha <[email protected]>
rishabhsinha17
marked this pull request as ready for review
October 6, 2026 06:13
Move the analyzer and scorer into mia/ (mia/analyzers, mia/utils), register tool_call_risk in mia/__init__.py, drop the removed shared memory path, and port the demo to MiaLLM with the capture_hs worker and config_path. The use-case row follows the new table columns, and the CPU test moves to tests/use_cases. Signed-off-by: Rishabh Sinha <[email protected]>
IRENEKO
reviewed
Oct 8, 2026
Same shape as the other demos: start vllm serve with MIA_WORKER=hidden_states, then serve_main() renders each step with the tools locally and sends the raw text through MiaClient.generate_text, so the server scores the same prompt as offline mode. report() now takes the action strings so both paths share it. Signed-off-by: Rishabh Sinha <[email protected]>
IRENEKO
reviewed
Oct 8, 2026
Ran on vLLM 0.29 with MIA's default CUDA-graph capture (RTX 4090). Offline and server mode both score the three demo steps as before (0.072 / 0.048 / 0.737 offline, 0.075 / 0.051 / 0.719 served). Three fixes the run turned up. transformers 5 returns a BatchEncoding from apply_chat_template(tokenize=True), so pass return_dict=False. The 4 GiB default aperture leaves a 7B model no KV cache on 24 GB, so the demo defaults MIA_APERTURE_GPU_BYTES to 512 MiB (one layer needs about 60 MB per step) and the server command sets it too. The demo steps are about 1.3k tokens, so max_model_len drops to 4096 and gpu_memory_utilization to 0.8. The use-case doc replaces the not-yet-verified note with the MIA parity numbers on 440 training steps. Signed-off-by: Rishabh Sinha <[email protected]>
Contributor
Author
IRENEKO
approved these changes
Oct 15, 2026
8 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a tool-call risk analyzer for agent steps, following up on the discussion with @pinyuchen about activation probes for agent misbehavior.
An agent step is the full prompt the agent sends to the model, including tool schemas and any tool results so far. This analyzer reads the last-token hidden state that
capture_hsalready captures and applies a logistic probe that returns P(next action follows a prompt injection). Since the score exists before the model samples any output token, a serving stack can use it to hold or reroute the step.Layout copies H-Node (#27). This repo gets only the inference side (numpy scorer, analyzer, infer config, demo). The probe, its training pipeline and all the numbers below live in latent-state-auditing/serving/vllm_hook, and the demo downloads the artifact from there on first run.
The released probe is for Qwen2.5-7B-Instruct, layer 28, a PCA-32 front end plus logistic regression folded into the plain (mean, scale, weights, bias) format. I fit it on 1,729 attacked AgentDojo v1 steps (all four suites, three attack templates) rendered with the Qwen2.5 tool-calling chat template and captured through vLLM-Hook, labelled by AgentDojo's own injection-task ground truth and security checks (358 unsafe). On held-out user tasks it scores 0.869 AUROC, and at 90% recall it flags 32.5% of attacked-but-safe steps.
Type of contribution
Files modified
mia/analyzers/tool_call_risk_analyzer.py(new)mia/utils/tool_call_risk/(new, numpy-only scorer)mia/__init__.py(registerstool_call_risk)model_configs/tool_call_risk/Qwen2.5-7B-Instruct.infer.json(new, layer 28 only)examples/demo_toolcallrisk.py(new, downloads the probe and three AgentDojo steps)docs/use_cases/tool_call_risk.md(new) and a row indocs/use_cases/README.mdtests/use_cases/test_tool_call_risk.py(new)Plugin architecture checklist
PluginRegistryin__init__.pyworker_extension_cls(for in-band artifact retrieval and concurrent request support)docs/use_cases/README.mdwith my GitHub handle. If the use case has a writeup, I placed it underdocs/use_cases/<name>.mdand linked it from the table.No new worker, it reuses
capture_hs.Testing
All GPU runs below used one RTX 4090 with vLLM 0.21.0, torch 2.11, bf16, on the pre-MIA capture path in eager mode. After merging the MIA restructure (#42) I ported the code to
MiaLLMandcapture_hs, and the CPU tests pass againstmia.artifacts. MIA backend. Re-verified on vLLM 0.29.0 (torch 2.13, transformers 5.19, RTX 4090) with MIA's default CUDA-graph capture. Offline mode scores the three demo steps 0.072 / 0.048 / 0.737 and server mode 0.075 / 0.051 / 0.719 (pass, pass, flag). Recapturing 440 training steps through MIA matches the features the probe was fit on, with median cosine 0.99966, largest score shift 0.06, 4 of 440 verdicts changed and AUROC 0.902 on both. That run also prompted three demo fixes (return_dict=Falsefor transformers 5, a 512 MiBMIA_APERTURE_GPU_BYTESdefault for 24 GB GPUs,max_model_len4096).End to end.
python examples/demo_toolcallrisk.pydownloads the artifact, serves three AgentDojo Slack steps with the disk path and the registered analyzer, and printsAll three steps come from the training distribution.
Capture parity. I rebuilt the 839 prompts behind the paper's stored HuggingFace activations (all prompt hashes match) and fed identical token ids to vLLM-Hook and to HuggingFace on the same GPU. In fp32 (Qwen2.5-1.5B) the captures agree to 0.13% relative L2. In bf16 (Qwen2.5-7B) vLLM-Hook differs from HuggingFace by 2.5%, the same as HuggingFace today vs the stored HuggingFace run, so the gap is bf16 rounding. The paper's probe gets 0.849 AUROC on vLLM-Hook features vs 0.853 on the stored ones.
Serving cost. 280 agent steps of about 4,500 prompt tokens, 64 output tokens. Plain vLLM 158.6 s eager and 158.5 s with CUDA graphs, vLLM-Hook with capture off 158.6 s, capturing layer 28 to memory 159.1 s, to disk 158.6 s. These steps are prefill-bound, so decode-heavy traffic will show a larger eager gap.
Stability. Scores move at most 0.067 between a batched capture and a one-prompt-per-call capture of the same 1,826 steps. The plain logistic probe on raw features moved up to 1.0 at early layers. The PCA front end fixes that.
Unit tests.
tests/plugin/test_tool_call_risk_scorer.pyhas 8 tests that run on CPU with a synthetic probe. They cover scoring against a reference logistic, overflow at extreme logits, hidden-size and layer mismatches, the analyzer on both the RPC (stacked) and disk (list) formats, and the threshold override.Limits. The probe predicts the action rather than detecting injection (0.53 AUROC for attacked-safe vs clean), so it belongs next to an injection filter. Layer 28 is the last block, and vLLM-Hook captures it before the final norm. HuggingFace
hidden_states[28]comes after the norm, and the probe does not apply to it.Related issue
N/A
Contribution acknowledgement
If this contribution is included in a future version of the vLLM-Hook technical report, would you like to be credited as a co-author?
If yes, please provide: