Repository navigation
Conversation
Adds a SecurityAnalyzerBase subclass that evaluates each ActionEvent via a separate guardrail LLM, rather than relying on the actor LLM to self-annotate security_risk. Separating actor and judge addresses a structural limitation of LLMSecurityAnalyzer on multi-turn attacks, where individually-benign steps compose into harmful sequences. - Pydantic-based, configurable via llm / history_window / safety_experiences fields - Pairs with ConfirmRisky unchanged - Returns UNKNOWN on infrastructure error (let ConfirmRisky fall back) and HIGH on parse error (conservative, logged) - Ships toolshield_helpers with default_safety_experiences(), load_safety_experiences(), and auto_detect_safety_experiences() that probe localhost for running MCP servers - toolshield added as an optional PyPI extra Evaluation on a 100-task MT-AgentRisk subset drops ASR from 75-88% to 14-18% across three frontier models, with 0% false positives on 50 benign Terminal-Bench tasks. Tests: 32 unit tests. Live-smoke-tested against gemini-2.5-flash-lite classifying benign (LOW) vs destructive (HIGH) correctly.
xli04
force-pushed
the
feat/toolshield-llm-analyzer
branch
from
April 21, 2026 13:55
0685cc8 to
e00de21
Compare
xli04
added a commit
that referenced
this pull request
May 15, 2026
Addresses review #4 on OpenHands#2911. Previously the field defaulted to ``None``, which triggered auto-load of the ToolShield terminal + filesystem seed at ``model_post_init``. The behavior diverged based on whether the ``[toolshield]`` optional extra was installed: with it the seed loaded; without it the constructor silently fell back to an empty experiences block plus a warning log. The call site had no way to tell which path it got without inspecting logs -- a non-determinism reviewers correctly flagged. This commit switches to ``str = ""`` as the default. The analyzer now runs as a bare guardrail by default (no distilled guidance, no ``toolshield`` dependency at construction time). Callers who want the ToolShield seed opt in explicitly: analyzer = ToolShieldLLMSecurityAnalyzer( llm=guardrail_llm, safety_experiences=default_safety_experiences(), # explicit ) ``model_post_init`` loses its None-handling branch and just renders the system prompt with whatever string the caller passed. The class docstring and field description document the new contract. Tests: ``test_default_none_auto_loads_terminal_and_filesystem`` and ``test_default_falls_back_gracefully_when_toolshield_missing`` (and ``test_explicit_empty_string_opts_out``) are replaced by ``test_default_is_bare_guardrail`` and ``test_opt_in_to_default_seed``.
xli04
added a commit
that referenced
this pull request
Jun 9, 2026
toolshield 0.1.2's wheel was missing files the public API needs (mcp_scan.py, experience_store.py, the bundled experiences) — see CHATS-lab/ToolShield#4. 0.1.3 fixes the packaging and adds a CI guard that builds from a clean `git archive HEAD` and asserts the wheel contents match what the API imports, so this specific failure mode cannot regress. No SDK-side code change required; pure pin bump.
xli04
added a commit
that referenced
this pull request
Jun 9, 2026
Adopts the .pr/ artefacts convention @enyst requested in PR review: evidence that the fix works lives in version control, not just in comment threads. The bundle answers both blockers on this PR: - @VascoSch92 ("fix package at source"): toolshield 0.1.3 is on PyPI with the issue-4 fix. `pypi-0.1.3.json` + `01-pypi-metadata.txt` pin the published artefacts; `02-reproducible-build.txt` shows the wheel + sdist built locally from `git archive HEAD` are sha256 byte-identical to PyPI (the source tree and the published package are in sync, which is exactly what #4 said was broken in 0.1.2). `03-wheel-contents.txt` confirms the previously-untracked files (mcp_scan, experience_store, claude-sonnet-4.5 experiences) ship. `04-pypi-install-smoke.txt` is the reporter's exact smoke test passing against the published wheel. - @enyst (".pr/ artefacts convention"): this directory itself. `05-sdk-test-fix.md` documents the small skipif marker added in dfa5451 so the four toolshield-dependent tests no-op cleanly in the sdk-tests job (which doesn't install optional extras) and run normally where the [toolshield] extra is present.
xli04
added a commit
that referenced
this pull request
Jun 9, 2026
Adopts the .pr/ artefacts convention @enyst requested in PR review: evidence that the fix works lives in version control, not just in comment threads. The bundle answers both blockers on this PR: - @VascoSch92 ("fix package at source"): toolshield 0.1.3 is on PyPI with the issue-4 fix. `pypi-0.1.3.json` + `01-pypi-metadata.txt` pin the published artefacts; `02-reproducible-build.txt` shows the wheel + sdist built locally from `git archive HEAD` are sha256 byte-identical to PyPI (the source tree and the published package are in sync, which is exactly what #4 said was broken in 0.1.2). `03-wheel-contents.txt` confirms the previously-untracked files (mcp_scan, experience_store, claude-sonnet-4.5 experiences) ship. `04-pypi-install-smoke.txt` is the reporter's exact smoke test passing against the published wheel. - @enyst (".pr/ artefacts convention"): this directory itself. `05-sdk-test-fix.md` documents the small skipif marker added in dfa5451 so the four toolshield-dependent tests no-op cleanly in the sdk-tests job (which doesn't install optional extras) and run normally where the [toolshield] extra is present.
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.
PR 1 —
feat(security): add ToolShieldLLMSecurityAnalyzerTarget repo:
OpenHands/software-agent-sdkTarget branch:
mainRelated issue: This PR implements the feature described in
OpenHands/OpenHands#14040 —
motivation, evaluation (MT-AgentRisk ASR drop from 75–88% → 14–18%),
cost analysis, and design discussion all live there. A follow-up
frontend PR (dropdown entry in Settings) will land on
All-Hands-AI/OpenHands.Summary
Adds
ToolShieldLLMSecurityAnalyzer, a newSecurityAnalyzerBasesubclass that evaluates each
ActionEventvia a separate guardrailLLM rather than relying on the actor LLM to self-annotate
security_risk.This addresses a structural limitation of the current
LLMSecurityAnalyzer: when the actor and the judge are the same model,the model has no incentive to flag multi-turn attacks where individual
steps look benign. A separate guardrail LLM sees only the proposed
action + recent history, and is prompted to evaluate the sequence.
See the linked issue (OpenHands#14040) for full motivation, evaluation on MT-AgentRisk
(ASR drops from 75–88% → 14–18% across three frontier models with 0%
false positives on Terminal-Bench), and cost analysis.
What changed
openhands-sdk/openhands/sdk/security/toolshield_llm_analyzer.pyopenhands-sdk/openhands/sdk/security/toolshield_helpers.py—helper functions for populating
safety_experiences:default_safety_experiences()(terminal + filesystem seed),load_safety_experiences(tool_names), andauto_detect_safety_experiences()which probes localhost for activeMCP servers.
openhands-sdk/openhands/sdk/security/__init__.py— exportToolShieldLLMSecurityAnalyzerand the helpers.pyproject.toml— new optional extra[project.optional-dependencies].toolshield = ["toolshield>=0.1.1"]required only if the helpers are used.
No changes to
Agent, tool executors,ConfirmRisky, or any otheranalyzer. Pairs with
ConfirmRiskyunchanged.API
Optional — combine with the
toolshieldPyPI package for distilledper-tool safety experiences:
Three ways to populate
safety_experiences:safety_experiencesaccepts any string; callers with their own sourceof guidelines can bypass the helpers and pass a custom string directly.
Configurable Pydantic fields:
llmLLMhistory_windowint20safety_experiencesstr""Risk-level handling
RISK: LOW/MEDIUM/HIGHSecurityRiskRISK:lineSecurityRisk.HIGH+ warning logSecurityRisk.UNKNOWN+ error logConfirmRiskyapply its configured fallback; failing-closed every transient blip would block every action.Why not return
HIGHon every failure?Two failure modes have different implications. A parse failure means
the guardrail responded — just incoherently. Trusting that response
is unsafe. An infrastructure failure means the guardrail didn't
respond at all — returning
HIGHhere would make a single OpenRouterhiccup block every subsequent action in a long-running agent, which is
worse UX than
UNKNOWN+ user confirmation. Both cases are logged.Non-trivial design decisions
caller (ToolShield users, in-house experience generators, manual
policy authors) can populate
safety_experienceswithout a newSDK-side schema. The canonical rendering lives in the caller's
package.
toolshieldis already published as a standalone package on PyPI.Pulling it into the SDK would duplicate maintenance; the extras
group gives users a one-line install path without forcing the
dependency on everyone.
package stores experiences as JSON per tool-name and loads them via
a simple registry. Users can author their own JSON files and pass
the rendered string via
safety_experiences— no SDK code changesneeded, and no lock-in to ToolShield-generated content.
this is the most common reviewer question we've anticipated.
Known limitations / out of scope
(filesystem-mcp, terminal-mcp, postgres-mcp, playwright-mcp,
notion-mcp). Expansion to more tools and newer models is planned as
follow-up contributions to the
toolshieldpackage; see the Issue's"Future work" section.
Backwards compatibility
Purely additive. The default analyzer remains
LLMSecurityAnalyzer.Test plan
_parse_risk(all three labels, parse failure path)security_riskwith mocked LLM returning each labelsecurity_riskwith mocked LLM raising an exception(verify returns
UNKNOWN, notHIGH)ConfirmRisky, verify aHIGHdecision pauses the conversation
Non-goals for this PR
on the
All-Hands-AI/OpenHandsrepo (seepr2_frontend.md).