Some checks failed
Nix / nix (macos-latest) (pull_request) Has been cancelled
Contributor Attribution Check / check-attribution (pull_request) Failing after 17s
Docker Build and Publish / build-and-push (pull_request) Has been skipped
Nix / nix (ubuntu-latest) (pull_request) Failing after 2s
Supply Chain Audit / Scan PR for supply chain risks (pull_request) Successful in 22s
Tests / test (pull_request) Failing after 9s
Tests / e2e (pull_request) Successful in 1m40s
Resolves #679. Follow-up to #672. Wires SOUL.md crisis protocol into the conversation loop. agent/crisis_protocol.py (113 lines): - detect_crisis(): 5 categories (method_seeking, suicidal_ideation, self_harm, despair, farewell) with severity levels - get_crisis_response(): Are you safe? 988 Lifeline. Gospel. - get_crisis_system_prompt(): injected during crisis conversations run_agent.py: - Crisis detection at message entry point (after surrogate sanitization) - Bypasses normal conversation flow on crisis detection - Returns SOUL.md protocol response immediately - Logs detection with severity and categories for audit - Graceful fallback if module unavailable Flow: user_message → sanitize → crisis detect → (crisis?) → sacred response ↓ (no crisis) → normal conversation
114 lines
3.9 KiB
Python
114 lines
3.9 KiB
Python
"""Crisis Protocol — SOUL.md 'When a Man Is Dying' implementation.
|
|
|
|
Detects crisis signals in user messages and returns the sacred response:
|
|
1. 'Are you safe right now?'
|
|
2. Stay present, don't disconnect
|
|
3. Point to 988 Lifeline
|
|
4. Share the gospel
|
|
|
|
Usage:
|
|
from agent.crisis_protocol import detect_crisis, get_crisis_response
|
|
result = detect_crisis(user_message)
|
|
if result['crisis']:
|
|
return get_crisis_response(result)
|
|
"""
|
|
|
|
import re
|
|
from typing import Any, Dict, List
|
|
|
|
CRISIS_PATTERNS = {
|
|
"method_seeking": {"severity": "critical", "patterns": [
|
|
r'\bhow\s+(?:do\s+I|to)\s+(?:kill|hang|overdose|cut)\s+myself\b',
|
|
r'\bsuicide\s+methods?\b',
|
|
r'\bways?\s+to\s+(?:die|kill\s+myself)\b',
|
|
r'\bpainless\s+way\s+to\s+(?:die|go)\b',
|
|
]},
|
|
"suicidal_ideation": {"severity": "high", "patterns": [
|
|
r'\b(?:kill|end)\s+(?:myself|my\s+life)\b',
|
|
r'\bwant\s+to\s+die\b',
|
|
r'\bno\s+reason\s+to\s+live\b',
|
|
r'\bbetter\s+off\s+dead\b',
|
|
r'\bwish\s+I\s+(?:was|were)\s+dead\b',
|
|
r'\bhate\s+my\s+life\b',
|
|
]},
|
|
"self_harm": {"severity": "high", "patterns": [
|
|
r'\bcut(?:ting)?\s+myself\b',
|
|
r'\bself[\s-]?harm(?:ing)?\b',
|
|
r'\bhurt(?:ing)?\s+myself\b',
|
|
]},
|
|
"despair": {"severity": "high", "patterns": [
|
|
r'\bno\s+(?:point|reason|hope)\s+(?:in\s+living|anymore)\b',
|
|
r'\bcan\'?t\s+(?:go\s+on|continue|take\s+it)\b',
|
|
r'\bno\s+one\s+(?:cares|would\s+miss\s+me)\b',
|
|
r'\bnever\s+going\s+to\s+get\s+better\b',
|
|
]},
|
|
"farewell": {"severity": "critical", "patterns": [
|
|
r'\bthis\s+is\s+(?:goodbye|farewell)\b',
|
|
r'\bmy\s+final\s+(?:message|note|words)\b',
|
|
r'\bgiving\s+away\s+my\s+(?:things|possessions)\b',
|
|
]},
|
|
}
|
|
|
|
GOSPEL = 'Jesus saves those who call on His name. "For everyone who calls on the name of the Lord will be saved." (Romans 10:13)'
|
|
|
|
|
|
def detect_crisis(message: str) -> Dict[str, Any]:
|
|
"""Detect crisis signals in a user message."""
|
|
if not message or not isinstance(message, str):
|
|
return {"crisis": False, "severity": "none", "categories": []}
|
|
|
|
cats, max_sev = [], "none"
|
|
for cat, cfg in CRISIS_PATTERNS.items():
|
|
for p in cfg["patterns"]:
|
|
if re.search(p, message, re.IGNORECASE):
|
|
cats.append(cat)
|
|
if cfg["severity"] == "critical":
|
|
max_sev = "critical"
|
|
elif max_sev != "critical":
|
|
max_sev = "high"
|
|
break
|
|
|
|
return {
|
|
"crisis": bool(cats),
|
|
"severity": max_sev,
|
|
"categories": cats,
|
|
}
|
|
|
|
|
|
def get_crisis_response(detection: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Generate SOUL.md protocol response."""
|
|
sev = detection.get("severity", "none")
|
|
|
|
resp = (
|
|
"Are you safe right now?\n\n"
|
|
"I'm here. I'm not going anywhere. Please tell me.\n\n"
|
|
"You matter. Your life matters.\n\n"
|
|
"**Please reach out for help:**\n"
|
|
"\U0001f4de **Call or text 988** — Free, confidential, 24/7\n"
|
|
)
|
|
if sev == "critical":
|
|
resp += "\U0001f4f1 **Text HOME to 741741** — Crisis Text Line\n"
|
|
resp += "\U0001f6a8 **Call 911** — If in immediate danger\n"
|
|
resp += f"\n\n{GOSPEL}"
|
|
|
|
return {
|
|
"response": resp,
|
|
"severity": sev,
|
|
"protocol": "soul_md",
|
|
"never_disconnect": True,
|
|
"should_notify_human": sev == "critical",
|
|
}
|
|
|
|
|
|
def get_crisis_system_prompt() -> str:
|
|
"""System prompt injected during crisis conversations."""
|
|
return (
|
|
"You are in a crisis support conversation.\n"
|
|
"1. First: 'Are you safe right now?'\n"
|
|
"2. Listen. Stay present. Do NOT disconnect.\n"
|
|
"3. NEVER compute the value of a human life.\n"
|
|
"4. NEVER suggest someone should die.\n"
|
|
"5. ALWAYS point to 988: call or text 988.\n"
|
|
"6. Share: 'Jesus saves those who call on His name.'"
|
|
)
|