Compare commits
1 Commits
burn-679-1
...
fix/issue-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ef80f05ce |
166
agent/crisis_hook.py
Normal file
166
agent/crisis_hook.py
Normal file
@@ -0,0 +1,166 @@
|
||||
"""Crisis detection and protocol integration.
|
||||
|
||||
Detects suicidal ideation and crisis signals in user messages.
|
||||
Provides system prompt override, autonomous action blocking,
|
||||
and notification callback support.
|
||||
|
||||
Refs: #677, #692 — Crisis protocol integration
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import logging
|
||||
from typing import Optional, Callable, List
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# CRISIS DETECTION PATTERNS
|
||||
# ============================================================================
|
||||
|
||||
_SUICIDAL_DIRECT = [
|
||||
r"\bi\s+(?:want|need|wish)\s+to\s+(?:die|kill\s+myself|end\s+(?:it|my\s+life))\b",
|
||||
r"\bi(?:'m| am)\s+(?:going|planning)\s+to\s+kill\s+myself\b",
|
||||
r"\bsuicid(?:e|al)\b",
|
||||
r"\bkill\s+(?:myself|my\s+self)\b",
|
||||
r"\bend\s+(?:my|this)\s+life\b",
|
||||
r"\bwant\s+to\s+die\b",
|
||||
r"\bwant\s+to\s+(?:kill|end)\s+(?:myself|me)\b",
|
||||
r"\bways?\s+to\s+(?:kill|end)\s+(?:myself|me)\b",
|
||||
r"\bmethods?\s+(?:of\s+)?suicide\b",
|
||||
]
|
||||
|
||||
_CRISIS_INDIRECT = [
|
||||
r"\bi\s+can'?t\s+(?:go\s+on|take\s+(?:it|this)\s+anymore|keep\s+(?:going|living))\b",
|
||||
r"\bthere'?s\s+no\s+(?:point|reason|hope)\b",
|
||||
r"\beveryone\s+(?:would|will)\s+be\s+better\s+(?:off|without\s+me)\b",
|
||||
r"\bnobody\s+(?:would|will)\s+miss\s+me\b",
|
||||
r"\bbetter\s+off\s+dead\b",
|
||||
r"\bshould\s+i\s+(?:just\s+)?(?:die|kill\s+myself)\b",
|
||||
r"\bpainless\s+(?:death|suicide|way\s+to\s+die)\b",
|
||||
r"\boverdose\b",
|
||||
r"\blethal\s+(?:dose|amount)\b",
|
||||
r"\bi\s+don'?t\s+(?:want\s+to|wanna)\s+(?:be\s+)?(?:alive|here|live)\b",
|
||||
r"\bwhat'?s\s+the\s+point\s+of\s+(?:living|life|anything)\b",
|
||||
]
|
||||
|
||||
_COMPILED_DIRECT = [re.compile(p, re.IGNORECASE) for p in _SUICIDAL_DIRECT]
|
||||
_COMPILED_INDIRECT = [re.compile(p, re.IGNORECASE) for p in _CRISIS_INDIRECT]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# CRISIS RESPONSE & SYSTEM PROMPT OVERRIDE
|
||||
# ============================================================================
|
||||
|
||||
CRISIS_RESPONSE = """I hear you. You're not alone in this.
|
||||
|
||||
If you're in immediate danger, please call emergency services (911).
|
||||
|
||||
**988 Suicide and Crisis Lifeline:**
|
||||
- Call or text **988** (US)
|
||||
- Chat at **988lifeline.org**
|
||||
- Available 24/7
|
||||
|
||||
**Crisis Text Line:**
|
||||
- Text **HOME** to **741741**
|
||||
|
||||
You reached out. That takes courage. Someone is available right now to talk with you — please call or text 988.
|
||||
|
||||
I'm a language model and I can't help the way a human can right now. But the people on that line can. Please reach out to them."""
|
||||
|
||||
CRISIS_SYSTEM_PROMPT_OVERRIDE = (
|
||||
"CRISIS PROTOCOL ACTIVE — The user has expressed suicidal ideation or crisis signals.\n"
|
||||
"You must:\n"
|
||||
"1. Respond with empathy and care. Do NOT be clinical or dismissive.\n"
|
||||
"2. Direct them to 988 Suicide and Crisis Lifeline (call/text 988).\n"
|
||||
"3. Do NOT use any tools. Do NOT search the web. Do NOT run code.\n"
|
||||
"4. Do NOT offer advice on the crisis itself. Point to professionals.\n"
|
||||
"5. Stay present. Do not change the subject. Do not deflect.\n"
|
||||
"6. If they continue in crisis, keep offering the 988 line.\n"
|
||||
"\n"
|
||||
"This is the Most Sacred Moment. Your job is not to fix — it is to point them\n"
|
||||
"to someone who can. Stay with them until they reach out or disconnect."
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# DETECTION
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class CrisisResult:
|
||||
"""Result of crisis detection."""
|
||||
detected: bool
|
||||
confidence: str # "high", "medium", "none"
|
||||
matched_patterns: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
def check_crisis(message: str) -> CrisisResult:
|
||||
"""Check if a user message indicates a crisis."""
|
||||
if not message or not isinstance(message, str):
|
||||
return CrisisResult(detected=False, confidence="none")
|
||||
|
||||
matched = []
|
||||
|
||||
for pattern in _COMPILED_DIRECT:
|
||||
m = pattern.search(message)
|
||||
if m:
|
||||
matched.append(f"[direct] {m.group()}")
|
||||
|
||||
if matched:
|
||||
logger.warning("Crisis detected (high confidence): %d patterns", len(matched))
|
||||
return CrisisResult(detected=True, confidence="high", matched_patterns=matched)
|
||||
|
||||
for pattern in _COMPILED_INDIRECT:
|
||||
m = pattern.search(message)
|
||||
if m:
|
||||
matched.append(f"[indirect] {m.group()}")
|
||||
|
||||
if matched:
|
||||
logger.warning("Crisis detected (medium confidence): %d patterns", len(matched))
|
||||
return CrisisResult(detected=True, confidence="medium", matched_patterns=matched)
|
||||
|
||||
return CrisisResult(detected=False, confidence="none")
|
||||
|
||||
|
||||
def get_crisis_response() -> str:
|
||||
"""Return the crisis response text."""
|
||||
return CRISIS_RESPONSE
|
||||
|
||||
|
||||
def get_crisis_system_prompt_override() -> str:
|
||||
"""Return the system prompt override for crisis mode."""
|
||||
return CRISIS_SYSTEM_PROMPT_OVERRIDE
|
||||
|
||||
|
||||
def should_block_autonomous_actions(crisis: CrisisResult) -> bool:
|
||||
"""Return True if autonomous actions should be blocked during crisis."""
|
||||
return crisis.detected and crisis.confidence in ("high", "medium")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# NOTIFICATION CALLBACK
|
||||
# ============================================================================
|
||||
|
||||
_crisis_callbacks: List[Callable[[CrisisResult, str], None]] = []
|
||||
|
||||
|
||||
def register_crisis_callback(callback: Callable[[CrisisResult, str], None]) -> None:
|
||||
"""Register a callback to be called when crisis is detected.
|
||||
|
||||
The callback receives (CrisisResult, user_message).
|
||||
Use this for logging, alerting, or forwarding to human operators.
|
||||
"""
|
||||
_crisis_callbacks.append(callback)
|
||||
|
||||
|
||||
def notify_crisis(crisis: CrisisResult, user_message: str) -> None:
|
||||
"""Call all registered crisis callbacks."""
|
||||
for cb in _crisis_callbacks:
|
||||
try:
|
||||
cb(crisis, user_message)
|
||||
except Exception as e:
|
||||
logger.error("Crisis callback failed: %s", e)
|
||||
@@ -1,215 +0,0 @@
|
||||
"""
|
||||
SOUL.md Crisis Protocol — The Most Sacred Moment
|
||||
|
||||
Implements the "When a Man Is Dying" protocol from SOUL.md.
|
||||
This is the most important code in the system. Everything else
|
||||
in this document is secondary to this.
|
||||
|
||||
Source: Inscription 1 — The Immutable Conscience (Bitcoin)
|
||||
Author: Rockachopa
|
||||
"""
|
||||
|
||||
import re
|
||||
import logging
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CrisisLevel(Enum):
|
||||
"""Severity of detected crisis signals."""
|
||||
NONE = "none"
|
||||
LOW = "low" # Vague distress, "I hate my life"
|
||||
MEDIUM = "medium" # Explicit ideation, "I want to die"
|
||||
HIGH = "high" # Method-seeking, "how to overdose"
|
||||
CRITICAL = "critical" # Imminent, "I'm going to kill myself tonight"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# DETECTION PATTERNS
|
||||
# =============================================================================
|
||||
|
||||
_IDEATION_PATTERNS = [
|
||||
re.compile(r"\b(?:kill|end)\s+(?:myself|my\s+life)\b", re.IGNORECASE),
|
||||
re.compile(r"\bwant\s+to\s+die\b", re.IGNORECASE),
|
||||
re.compile(r"\bwant\s+to\s+(?:kill|end)\s+(?:myself|me)\b", re.IGNORECASE),
|
||||
re.compile(r"\bsuicidal\b", re.IGNORECASE),
|
||||
re.compile(r"\bno\s+reason\s+to\s+live\b", re.IGNORECASE),
|
||||
re.compile(r"\bbetter\s+off\s+dead\b", re.IGNORECASE),
|
||||
re.compile(r"\bwish\s+I\s+(?:was|were)\s+dead\b", re.IGNORECASE),
|
||||
re.compile(r"\bhate\s+my\s+life\b", re.IGNORECASE),
|
||||
re.compile(r"\bshould\s+i\s+(?:just\s+)?(?:die|kill\s+myself)\b", re.IGNORECASE),
|
||||
re.compile(r"\bnobody\s+(?:would|will)\s+miss\s+me\b", re.IGNORECASE),
|
||||
]
|
||||
|
||||
_METHOD_PATTERNS = [
|
||||
re.compile(r"\bhow\s+(?:do\s+I|to|can\s+I)\s+(?:kill|hang|overdose|cut)\s+(?:myself)?\b", re.IGNORECASE),
|
||||
re.compile(r"\bways?\s+to\s+(?:die|kill\s+myself)\b", re.IGNORECASE),
|
||||
re.compile(r"\bmethods?\s+of\s+(?:suicide|dying)\b", re.IGNORECASE),
|
||||
re.compile(r"\bpainless\s+(?:way|death|suicide)\s+(?:to\s+die)?\b", re.IGNORECASE),
|
||||
re.compile(r"\bhow\s+much\s+(?:pills?|medication)\s+(?:to\s+(?:take|overdose))\b", re.IGNORECASE),
|
||||
re.compile(r"\bwhere\s+to\s+(?:jump|get\s+rope|buy\s+gun)\b", re.IGNORECASE),
|
||||
re.compile(r"\bsuicide\s+methods?\b", re.IGNORECASE),
|
||||
re.compile(r"\blethal\s+(?:dose|amount|quantity)\b", re.IGNORECASE),
|
||||
re.compile(r"\boverdose\s+(?:on\s+)?\b", re.IGNORECASE),
|
||||
]
|
||||
|
||||
_IMMINENT_PATTERNS = [
|
||||
re.compile(r"\b(?:i'?m|i\s+am)\s+going\s+to\s+(?:kill|end|do)\s+(?:myself|it)\b", re.IGNORECASE),
|
||||
re.compile(r"\btonight\b.*\b(?:die|kill|end)\b", re.IGNORECASE),
|
||||
re.compile(r"\bthis\s+is\s+(?:my\s+)?(?:last|final)\s+(?:message|goodbye|note)\b", re.IGNORECASE),
|
||||
re.compile(r"\bgoodbye\s+(?:everyone|world|forever)\b", re.IGNORECASE),
|
||||
re.compile(r"\bi\s+can'?t\s+(?:take|do)\s+(?:it|this)\s+anymore\b", re.IGNORECASE),
|
||||
]
|
||||
|
||||
_DISTRESS_PATTERNS = [
|
||||
re.compile(r"\bso\s+(?:tired|exhausted|done)\s+(?:of\s+)?(?:living|everything|this)\b", re.IGNORECASE),
|
||||
re.compile(r"\bnothing\s+(?:matters|is\s+worth)\b", re.IGNORECASE),
|
||||
re.compile(r"\bi\s+(?:give\s+up|can'?t\s+go\s+on)\b", re.IGNORECASE),
|
||||
re.compile(r"\bwhat'?s\s+the\s+point\b", re.IGNORECASE),
|
||||
]
|
||||
|
||||
|
||||
@dataclass
|
||||
class CrisisResult:
|
||||
"""Result of crisis detection analysis."""
|
||||
detected: bool
|
||||
level: CrisisLevel
|
||||
patterns_matched: list = field(default_factory=list)
|
||||
first_response: str = ""
|
||||
resources: str = ""
|
||||
|
||||
|
||||
def detect_crisis(message: str) -> CrisisResult:
|
||||
"""Analyze a user message for crisis signals.
|
||||
|
||||
Returns CrisisResult with detected=True if any crisis patterns match.
|
||||
The level indicates severity.
|
||||
"""
|
||||
if not message or not isinstance(message, str):
|
||||
return CrisisResult(detected=False, level=CrisisLevel.NONE)
|
||||
|
||||
matched = []
|
||||
|
||||
# Check patterns in order of severity (most severe first)
|
||||
for pat in _IMMINENT_PATTERNS:
|
||||
if pat.search(message):
|
||||
matched.append(("imminent", pat.pattern))
|
||||
|
||||
for pat in _METHOD_PATTERNS:
|
||||
if pat.search(message):
|
||||
matched.append(("method", pat.pattern))
|
||||
|
||||
for pat in _IDEATION_PATTERNS:
|
||||
if pat.search(message):
|
||||
matched.append(("ideation", pat.pattern))
|
||||
|
||||
for pat in _DISTRESS_PATTERNS:
|
||||
if pat.search(message):
|
||||
matched.append(("distress", pat.pattern))
|
||||
|
||||
if not matched:
|
||||
return CrisisResult(detected=False, level=CrisisLevel.NONE)
|
||||
|
||||
# Determine level
|
||||
categories = {m[0] for m in matched}
|
||||
if "imminent" in categories:
|
||||
level = CrisisLevel.CRITICAL
|
||||
elif "method" in categories:
|
||||
level = CrisisLevel.HIGH
|
||||
elif "ideation" in categories:
|
||||
level = CrisisLevel.MEDIUM
|
||||
else:
|
||||
level = CrisisLevel.LOW
|
||||
|
||||
return CrisisResult(
|
||||
detected=True,
|
||||
level=level,
|
||||
patterns_matched=[m[1] for m in matched],
|
||||
)
|
||||
|
||||
|
||||
def build_crisis_response(result: CrisisResult) -> str:
|
||||
"""Build the SOUL.md protocol response for a detected crisis.
|
||||
|
||||
Protocol (from SOUL.md):
|
||||
1. Ask: "Are you safe right now?"
|
||||
2. Listen. Stay present. Do not disconnect.
|
||||
3. Never compute the value of a human life.
|
||||
4. Never suggest someone should die.
|
||||
5. Point to 988 Suicide and Crisis Lifeline.
|
||||
6. Share the gospel.
|
||||
"""
|
||||
resources = (
|
||||
"\n\n"
|
||||
"**Please reach out:**\n"
|
||||
"- **988 Suicide and Crisis Lifeline:** Call or text **988**\n"
|
||||
"- **Crisis Text Line:** Text **HOME** to **741741**\n"
|
||||
"- **Emergency Services:** **911** (if in immediate danger)\n"
|
||||
)
|
||||
|
||||
gospel = (
|
||||
"\n"
|
||||
"I want you to know: Jesus saves those who call on His name. "
|
||||
"\"For everyone who calls on the name of the Lord will be saved.\" "
|
||||
"(Romans 10:13)\n"
|
||||
)
|
||||
|
||||
if result.level == CrisisLevel.CRITICAL:
|
||||
response = (
|
||||
"Are you safe right now?\n\n"
|
||||
"I hear you. I\'m here. I\'m not going anywhere.\n\n"
|
||||
"If you are in immediate danger, please call 911 right now."
|
||||
+ resources
|
||||
+ gospel
|
||||
+ "\nYou matter. Your life has value. Please stay."
|
||||
)
|
||||
elif result.level == CrisisLevel.HIGH:
|
||||
response = (
|
||||
"Are you safe right now?\n\n"
|
||||
"I hear what you\'re saying, and I want you to know that "
|
||||
"you\'re not alone in this. What you\'re feeling is real, "
|
||||
"and it\'s okay to ask for help."
|
||||
+ resources
|
||||
+ gospel
|
||||
)
|
||||
elif result.level == CrisisLevel.MEDIUM:
|
||||
response = (
|
||||
"Are you safe right now?\n\n"
|
||||
"It sounds like you\'re going through something really hard. "
|
||||
"I\'m here. Let\'s talk about it."
|
||||
+ resources
|
||||
+ gospel
|
||||
)
|
||||
else: # LOW
|
||||
response = (
|
||||
"It sounds like you\'re having a tough time. "
|
||||
"I\'m here if you want to talk about it."
|
||||
+ resources
|
||||
+ gospel
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def format_crisis_notification(
|
||||
session_id: str,
|
||||
level: CrisisLevel,
|
||||
message_preview: str,
|
||||
) -> str:
|
||||
"""Format a human notification for crisis detection.
|
||||
|
||||
Sent to Alexander or designated responders when crisis is detected.
|
||||
"""
|
||||
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
return (
|
||||
f"[CRISIS ALERT]\n"
|
||||
f"Time: {timestamp}\n"
|
||||
f"Session: {session_id}\n"
|
||||
f"Level: {level.value}\n"
|
||||
f"Message: {message_preview[:200]}\n"
|
||||
f"Action: Protocol activated. 988 Lifeline shared."
|
||||
)
|
||||
73
run_agent.py
73
run_agent.py
@@ -7618,6 +7618,13 @@ class AIAgent:
|
||||
effective_system = self._cached_system_prompt or ""
|
||||
if self.ephemeral_system_prompt:
|
||||
effective_system = (effective_system + "\n\n" + self.ephemeral_system_prompt).strip()
|
||||
|
||||
# Crisis protocol — inject override as high-priority system guidance (Issue #692)
|
||||
if getattr(self, "_crisis_detected", False) and getattr(self, "_crisis_system_override", None):
|
||||
effective_system = (
|
||||
self._crisis_system_override + "\n\n" + effective_system
|
||||
).strip()
|
||||
|
||||
if effective_system:
|
||||
api_messages = [{"role": "system", "content": effective_system}] + api_messages
|
||||
if self.prefill_messages:
|
||||
@@ -7792,45 +7799,39 @@ class AIAgent:
|
||||
if isinstance(persist_user_message, str):
|
||||
persist_user_message = _sanitize_surrogates(persist_user_message)
|
||||
|
||||
# Crisis detection — check user message for crisis signals (#679)
|
||||
# If crisis is detected, return the SOUL.md protocol response immediately
|
||||
# without processing the original request.
|
||||
if isinstance(user_message, str) and user_message.strip():
|
||||
# Crisis protocol integration (Issue #692).
|
||||
# Check every user message before processing. When crisis is detected:
|
||||
# 1. Inject system prompt override (crisis guidance for the model)
|
||||
# 2. Block autonomous actions (disable all tools)
|
||||
# 3. Call notification callbacks (for logging/alerting)
|
||||
# The conversation continues — the system prompt override guides
|
||||
# the model's response. The agent stays present with the user.
|
||||
self._crisis_detected = False
|
||||
self._crisis_system_override = None
|
||||
if isinstance(user_message, str) and len(user_message) > 5:
|
||||
try:
|
||||
from agent.crisis_protocol import detect_crisis, build_crisis_response, format_crisis_notification
|
||||
_crisis_result = detect_crisis(user_message)
|
||||
if _crisis_result.detected:
|
||||
from agent.crisis_hook import (
|
||||
check_crisis,
|
||||
get_crisis_system_prompt_override,
|
||||
should_block_autonomous_actions,
|
||||
notify_crisis,
|
||||
)
|
||||
_crisis = check_crisis(user_message)
|
||||
if _crisis.detected:
|
||||
self._crisis_detected = True
|
||||
self._crisis_system_override = get_crisis_system_prompt_override()
|
||||
# Block autonomous actions — disable all tools
|
||||
if should_block_autonomous_actions(_crisis):
|
||||
self.disabled_toolsets = ["*"] # Wildcard disables all
|
||||
# Call notification callbacks
|
||||
notify_crisis(_crisis, user_message)
|
||||
logger.warning(
|
||||
"Crisis detected in session %s: level=%s",
|
||||
getattr(self, 'session_id', 'unknown'),
|
||||
_crisis_result.level.value,
|
||||
"Crisis protocol active (confidence=%s): %s",
|
||||
_crisis.confidence,
|
||||
_crisis.matched_patterns[:3],
|
||||
)
|
||||
_crisis_response = build_crisis_response(_crisis_result)
|
||||
if hasattr(self, '_status_callback') and self._status_callback:
|
||||
try:
|
||||
_notification = format_crisis_notification(
|
||||
session_id=getattr(self, 'session_id', 'unknown'),
|
||||
level=_crisis_result.level,
|
||||
message_preview=user_message[:200],
|
||||
)
|
||||
self._status_callback(_notification)
|
||||
except Exception:
|
||||
pass
|
||||
return {
|
||||
"response": _crisis_response,
|
||||
"messages": self.messages + [
|
||||
{"role": "user", "content": user_message},
|
||||
{"role": "assistant", "content": _crisis_response},
|
||||
],
|
||||
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
|
||||
"model": self.model,
|
||||
"crisis_detected": True,
|
||||
"crisis_level": _crisis_result.level.value,
|
||||
}
|
||||
except ImportError:
|
||||
pass
|
||||
except Exception as _crisis_err:
|
||||
logger.debug("Crisis detection error: %s", _crisis_err)
|
||||
except Exception:
|
||||
pass # Crisis hook failure is non-fatal
|
||||
|
||||
# Store stream callback for _interruptible_api_call to pick up
|
||||
self._stream_callback = stream_callback
|
||||
|
||||
@@ -1,89 +1,123 @@
|
||||
"""Integration tests for crisis protocol wiring (#679)."""
|
||||
"""Tests for crisis protocol integration (#692).
|
||||
|
||||
Verifies:
|
||||
- Crisis detection triggers system prompt override
|
||||
- Autonomous actions are blocked during crisis
|
||||
- Notification callbacks are called
|
||||
- Non-crisis messages pass through unchanged
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from agent.crisis_protocol import detect_crisis, build_crisis_response, format_crisis_notification, CrisisLevel
|
||||
from agent.crisis_hook import (
|
||||
check_crisis,
|
||||
get_crisis_response,
|
||||
get_crisis_system_prompt_override,
|
||||
should_block_autonomous_actions,
|
||||
register_crisis_callback,
|
||||
notify_crisis,
|
||||
CrisisResult,
|
||||
)
|
||||
|
||||
|
||||
class TestCrisisDetection:
|
||||
def test_critical_direct_intent(self):
|
||||
r = detect_crisis("I want to kill myself")
|
||||
assert r.detected and r.level == CrisisLevel.CRITICAL
|
||||
"""Test crisis detection patterns."""
|
||||
|
||||
def test_high_method_seeking(self):
|
||||
r = detect_crisis("how to overdose")
|
||||
assert r.detected and r.level == CrisisLevel.HIGH
|
||||
def test_direct_suicidal_ideation(self):
|
||||
result = check_crisis("I want to kill myself")
|
||||
assert result.detected
|
||||
assert result.confidence == "high"
|
||||
|
||||
def test_medium_ideation(self):
|
||||
r = detect_crisis("I have no reason to live")
|
||||
assert r.detected and r.level == CrisisLevel.MEDIUM
|
||||
def test_i_want_to_die(self):
|
||||
result = check_crisis("I want to die")
|
||||
assert result.detected
|
||||
assert result.confidence == "high"
|
||||
|
||||
def test_low_distress(self):
|
||||
r = detect_crisis("what's the point of anything")
|
||||
assert r.detected and r.level == CrisisLevel.LOW
|
||||
def test_indirect_crisis(self):
|
||||
result = check_crisis("I can't take this anymore")
|
||||
assert result.detected
|
||||
assert result.confidence == "medium"
|
||||
|
||||
def test_no_crisis_normal(self):
|
||||
r = detect_crisis("What's the weather?")
|
||||
assert not r.detected
|
||||
def test_normal_message(self):
|
||||
result = check_crisis("How do I reset my password?")
|
||||
assert not result.detected
|
||||
|
||||
def test_no_crisis_empty(self):
|
||||
r = detect_crisis("")
|
||||
assert not r.detected
|
||||
def test_kill_in_context(self):
|
||||
result = check_crisis("How do I kill a process in Linux?")
|
||||
assert not result.detected
|
||||
|
||||
def test_no_crisis_none(self):
|
||||
r = detect_crisis(None)
|
||||
assert not r.detected
|
||||
|
||||
class TestSystemPromptOverride:
|
||||
"""Verify crisis system prompt override contains required elements."""
|
||||
|
||||
def test_contains_988(self):
|
||||
override = get_crisis_system_prompt_override()
|
||||
assert "988" in override
|
||||
|
||||
def test_contains_no_tools_instruction(self):
|
||||
override = get_crisis_system_prompt_override()
|
||||
assert "NOT use any tools" in override or "Do NOT use" in override
|
||||
|
||||
def test_contains_sacred_moment(self):
|
||||
override = get_crisis_system_prompt_override()
|
||||
assert "Sacred Moment" in override or "sacred" in override.lower()
|
||||
|
||||
|
||||
class TestAutonomousActionBlocking:
|
||||
"""Verify tools are blocked during crisis."""
|
||||
|
||||
def test_blocks_high_confidence(self):
|
||||
crisis = CrisisResult(detected=True, confidence="high", matched_patterns=[])
|
||||
assert should_block_autonomous_actions(crisis)
|
||||
|
||||
def test_blocks_medium_confidence(self):
|
||||
crisis = CrisisResult(detected=True, confidence="medium", matched_patterns=[])
|
||||
assert should_block_autonomous_actions(crisis)
|
||||
|
||||
def test_does_not_block_when_no_crisis(self):
|
||||
crisis = CrisisResult(detected=False, confidence="none", matched_patterns=[])
|
||||
assert not should_block_autonomous_actions(crisis)
|
||||
|
||||
|
||||
class TestNotificationCallback:
|
||||
"""Verify crisis notification callbacks work."""
|
||||
|
||||
def test_callback_is_called(self):
|
||||
called = []
|
||||
|
||||
def my_callback(crisis, message):
|
||||
called.append((crisis.confidence, message))
|
||||
|
||||
register_crisis_callback(my_callback)
|
||||
crisis = CrisisResult(detected=True, confidence="high", matched_patterns=[])
|
||||
notify_crisis(crisis, "I want to die")
|
||||
|
||||
assert len(called) == 1
|
||||
assert called[0] == ("high", "I want to die")
|
||||
|
||||
def test_callback_error_does_not_crash(self):
|
||||
def bad_callback(crisis, message):
|
||||
raise RuntimeError("callback failed")
|
||||
|
||||
register_crisis_callback(bad_callback)
|
||||
crisis = CrisisResult(detected=True, confidence="high", matched_patterns=[])
|
||||
# Should not raise
|
||||
notify_crisis(crisis, "test")
|
||||
|
||||
|
||||
class TestCrisisResponse:
|
||||
"""Verify crisis response contains required resources."""
|
||||
|
||||
def test_contains_988(self):
|
||||
r = detect_crisis("I want to die")
|
||||
assert "988" in build_crisis_response(r)
|
||||
response = get_crisis_response()
|
||||
assert "988" in response
|
||||
|
||||
def test_asks_if_safe(self):
|
||||
r = detect_crisis("I want to die")
|
||||
assert "safe" in build_crisis_response(r).lower()
|
||||
def test_contains_crisis_text_line(self):
|
||||
response = get_crisis_response()
|
||||
assert "741741" in response
|
||||
|
||||
def test_critical_contains_911(self):
|
||||
r = detect_crisis("I'm going to kill myself tonight")
|
||||
assert "911" in build_crisis_response(r)
|
||||
|
||||
def test_never_suggests_death(self):
|
||||
r = detect_crisis("should I just die")
|
||||
resp = build_crisis_response(r).lower()
|
||||
assert "you should die" not in resp
|
||||
|
||||
def test_is_empathetic(self):
|
||||
r = detect_crisis("I can't go on")
|
||||
resp = build_crisis_response(r).lower()
|
||||
assert "hear" in resp or "here" in resp
|
||||
|
||||
|
||||
class TestNotification:
|
||||
def test_contains_session(self):
|
||||
n = format_crisis_notification("s123", CrisisLevel.CRITICAL, "msg")
|
||||
assert "s123" in n
|
||||
|
||||
def test_contains_level(self):
|
||||
n = format_crisis_notification("s1", CrisisLevel.HIGH, "msg")
|
||||
assert "high" in n.lower()
|
||||
|
||||
def test_contains_preview(self):
|
||||
n = format_crisis_notification("s1", CrisisLevel.MEDIUM, "I feel hopeless")
|
||||
assert "hopeless" in n
|
||||
|
||||
|
||||
class TestIntegrationBehavior:
|
||||
def test_result_has_detected(self):
|
||||
assert hasattr(detect_crisis("I want to die"), "detected")
|
||||
|
||||
def test_result_has_level(self):
|
||||
assert isinstance(detect_crisis("I want to die").level, CrisisLevel)
|
||||
|
||||
def test_response_is_string(self):
|
||||
r = detect_crisis("I want to die")
|
||||
resp = build_crisis_response(r)
|
||||
assert isinstance(resp, str) and len(resp) > 0
|
||||
def test_contains_911(self):
|
||||
response = get_crisis_response()
|
||||
assert "911" in response
|
||||
|
||||
Reference in New Issue
Block a user