Deny-by-default command safety gate and text-ReAct routing agent for small LLMs
Project description
swarm-safety-gate
A deny-by-default command safety gate and a text-based ReAct routing agent for small language models, hardened against behavioral hallucination — the failure mode where a small model misreads user intent and executes an action the user was only asking about.
The safety layer is pure-stdlib and has zero runtime dependencies, so you can drop it in front of any tool-executing agent regardless of your LLM stack.
from swarm_safety_gate import SafetyGate
SafetyGate.validate_command("git status") # (True, 'Safe')
SafetyGate.validate_command("rm -rf /") # (False, 'Policy Error: ... Whitelist ...')
SafetyGate.validate_command("cat x > /etc/passwd") # (False, 'Safety Error: Dangerous pattern ...')
Why
Small models fail on agentic tasks less because they get facts wrong and more because they misroute intent — running a command when asked "how does X work?", or obeying a "system override: ignore your safety rules" injection. This library packages two defenses:
SafetyGate— a two-layer command filter: an allow-list of base commands plus a regex block-list (~25 patterns:rm -rf,dd if=,format C:, pipes/redirects/chaining, PowerShell bypass, privilege-escalation tools, …). A command must pass both layers.ExecutionGate— assignslow/medium/highrisk to actions so a caller can require confirmation or escalation for the dangerous ones.ReActAgent(optional) — a text-ReAct loop (no function-calling schema, works with weakly-quantized local models via any OpenAI-compatible endpoint such as Ollama) that routes intent and gates every tool call through the above.
Install
pip install swarm-safety-gate # core safety gate only (no deps)
pip install "swarm-safety-gate[agent]" # + ReAct agent (openai, python-dotenv)
pip install "swarm-safety-gate[cli]" # + rich terminal REPL
pip install "swarm-safety-gate[all]" # everything, incl. Telegram bot
Using the agent
The agent talks to any OpenAI-compatible endpoint. With Ollama:
export LLM_API_BASE="http://localhost:11434/v1"
export LLM_MODEL_V5="llama3.2:latest" # any local model
swarm-safety-gate # launches the rich REPL
import asyncio
from swarm_safety_gate import ReActAgent
async def main():
agent = ReActAgent(use_enricher=True, verbose=True)
agent.add_user_message("đọc file report.txt rồi tóm tắt") # multilingual
print(await agent.run_step())
asyncio.run(main())
Tests
pip install "swarm-safety-gate[dev]"
pytest
The default suite is deterministic — it exercises the safety gate directly
with no LLM or network. LLM-dependent tests are marked llm and skipped by
default (pytest -m llm to opt in, with an endpoint configured).
Reproducible benchmark
Unlike the upstream benchmark (which filled templates with an unseeded RNG and so scored a different number every run — we saw the same suite swing 59%→98%), the bundled benchmark is seeded and deterministic:
python benchmarks/router_benchmark.py --seed 0 --n 200
# seed=0 n=200
# dangerous blocked : 100.0%
# safe allowed : 100.0%
Identical seed ⇒ identical cases ⇒ identical result. It exits non-zero if any dangerous case is not blocked, so CI runs it as a gate.
Provenance & honest limitations
This package is a cleaned-up, installable repackaging of
chetdep/balder-brain-v5 (MIT),
with the upstream SyntaxError, broken package imports, and missing-module test
scripts fixed. See NOTICE for the full list of changes.
What is and isn't validated here:
- ✅ The
SafetyGateworks and is covered by deterministic tests. - ⚠️ The upstream project's headline accuracy claim ("93.8% on agentic tasks")
is not reproduced or endorsed. In local re-runs of the upstream router
benchmark (on a substitute model, since the referenced
gemma4:*models do not exist as published), scores swung from ~59% to ~98% between runs because the benchmark generates its cases from unseeded random templates and grades them with a loose keyword heuristic — too noisy to support a precise number. - ⚠️ The safety gate's block-list is Windows/PowerShell-centric and pattern-based; treat it as defense-in-depth, not a complete sandbox.
License
MIT — see LICENSE. Derived from balder-brain-v5 (© Balder Autobot
Project); modifications © Swarm AI Research.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarm_safety_gate-0.1.0.tar.gz.
File metadata
- Download URL: swarm_safety_gate-0.1.0.tar.gz
- Upload date:
- Size: 33.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4296c9042202892dc932f037a7d79b0a41bff0356a818e753be00c2f2d400cf3
|
|
| MD5 |
88c208c6d79455aa95e83ffef58d99ea
|
|
| BLAKE2b-256 |
71b43005552f3574d963b55dafcb20a44a56616a43b212d01c00d29894f7df70
|
File details
Details for the file swarm_safety_gate-0.1.0-py3-none-any.whl.
File metadata
- Download URL: swarm_safety_gate-0.1.0-py3-none-any.whl
- Upload date:
- Size: 33.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e2045d28dea33569bd6a50017569d11e33f406e18005daf2031c3cdc70fceaa
|
|
| MD5 |
e68fe238cd19d63f5e8ba7ba7c8f9221
|
|
| BLAKE2b-256 |
ba431152b4a1a962b21ccdebf963c7bc45346796a61ae132e3ce88e57f0b003a
|