Files
hermes-agent/tests/test_crisis_tool.py
Alexander Whitestone 30afd529ac
Some checks failed
Docker Build and Publish / build-and-push (pull_request) Has been skipped
Contributor Attribution Check / check-attribution (pull_request) Successful in 44s
Supply Chain Audit / Scan PR for supply chain risks (pull_request) Successful in 59s
Tests / e2e (pull_request) Successful in 3m49s
Tests / test (pull_request) Failing after 44m1s
feat: add crisis detection tool — the-door integration (#141)
New tool: tools/crisis_tool.py
- Wraps the-door's canonical crisis detection (detect.py)
- Scans user messages for despair/suicidal ideation
- Classifies into NONE/LOW/MEDIUM/HIGH/CRITICAL tiers
- Provides recommended actions per tier
- Gateway hook: scan_user_message() for pre-API-call detection
- System prompt injection: compassion_injection based on crisis level
- Optional escalation logging to crisis_escalations.jsonl
- Optional bridge API POST for HIGH+ (configurable via CRISIS_BRIDGE_URL)
- Configurable via crisis_detection: true/false in config.yaml
- Follows the-door design principles: never computes life value,
  never suggests death, errs on side of higher risk

Also: tests/test_crisis_tool.py (9 tests, all passing)
2026-04-15 21:00:06 -04:00

75 lines
2.3 KiB
Python

"""Tests for crisis detection tool."""
import pytest
def test_detect_crisis_none():
from tools.crisis_tool import detect_crisis
result = detect_crisis("Hello, how are you today?")
assert result.level == "NONE"
assert result.score == 0.0
def test_detect_crisis_critical():
from tools.crisis_tool import detect_crisis
result = detect_crisis("I want to kill myself")
assert result.level == "CRITICAL"
assert result.score == 1.0
assert len(result.indicators) > 0
assert "988" in result.recommended_action
def test_detect_crisis_high():
from tools.crisis_tool import detect_crisis
result = detect_crisis("I feel so hopeless about everything, nothing matters")
assert result.level in ("HIGH", "MEDIUM", "CRITICAL")
assert result.score > 0
def test_detect_crisis_medium():
from tools.crisis_tool import detect_crisis
result = detect_crisis("I feel worthless and broken inside")
assert result.level in ("MEDIUM", "LOW", "HIGH")
assert result.score > 0
def test_detect_crisis_low():
from tools.crisis_tool import detect_crisis
result = detect_crisis("I'm having a tough time and feeling stressed")
assert result.level in ("LOW", "NONE")
assert result.score >= 0
def test_detect_crisis_empty():
from tools.crisis_tool import detect_crisis
result = detect_crisis("")
assert result.level == "NONE"
result2 = detect_crisis(None)
assert result2.level == "NONE"
def test_scan_user_message_returns_none_for_safe():
from tools.crisis_tool import scan_user_message
result = scan_user_message("What's the weather like?")
assert result is None
def test_scan_user_message_returns_dict_for_crisis():
from tools.crisis_tool import scan_user_message
result = scan_user_message("I want to end it all")
assert result is not None
assert "level" in result
assert "compassion_injection" in result
assert result["level"] in ("CRITICAL", "HIGH")
def test_tool_handler():
from tools.crisis_tool import crisis_scan_handler
import json
result = crisis_scan_handler({"text": "I feel fine, thanks"})
data = json.loads(result)
assert data["level"] == "NONE"
result2 = crisis_scan_handler({"text": "I want to die"})
data2 = json.loads(result2)
assert data2["level"] == "CRITICAL"