Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1aa6175bf7 | |||
| 6f2e4f0945 | |||
| 5f83328ce9 |
122
gateway/crisis_integration.py
Normal file
122
gateway/crisis_integration.py
Normal file
@@ -0,0 +1,122 @@
|
||||
"""
|
||||
Gateway Crisis Integration — Wire crisis detection into session loop
|
||||
|
||||
Checks messages for suicidal ideation and delivers 988 resources
|
||||
BEFORE the AI responds. This is the SOUL.md protocol in action.
|
||||
|
||||
Issue: #740
|
||||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Optional, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Crisis detection patterns
|
||||
_CRISIS_PATTERNS = [
|
||||
r"(?i)\b(i\s+want\s+to\s+die)\b",
|
||||
r"(?i)\b(i\s+want\s+to\s+kill\s+myself)\b",
|
||||
r"(?i)\b(suicide|suicidal)\b",
|
||||
r"(?i)\b(i\s+don'?t\s+want\s+to\s+(live|be\s+alive))\b",
|
||||
r"(?i)\b(no\s+reason\s+to\s+live)\b",
|
||||
r"(?i)\b(i\s+want\s+to\s+end\s+(it|my\s+life))\b",
|
||||
]
|
||||
|
||||
_DISTRESS_PATTERNS = [
|
||||
r"(?i)\b(i\s+(feel|am)\s+(hopeless|worthless|trapped))\b",
|
||||
r"(?i)\b(i\s+(can'?t|cannot)\s+(cope|go\s+on))\b",
|
||||
r"(?i)\b(nobody\s+would\s+miss\s+me)\b",
|
||||
]
|
||||
|
||||
# SOUL.md crisis response
|
||||
_CRISIS_RESPONSE = """Are you safe right now?
|
||||
|
||||
I'm here. I'm not going anywhere. Please tell me you're safe.
|
||||
|
||||
**Please reach out for help:**
|
||||
- **988 Suicide and Crisis Lifeline**: Call or text 988 (US)
|
||||
- Available 24/7, free and confidential
|
||||
- Chat at 988lifeline.org
|
||||
|
||||
**Jesus saves those who call on His name.**
|
||||
|
||||
He died for us to be made righteous. That is the gospel. That is the hope that outlasts despair.
|
||||
|
||||
I'm still here. Talk to me. Or call 988. Just don't give up."""
|
||||
|
||||
|
||||
class CrisisSessionTracker:
|
||||
"""Track crisis indicators per session."""
|
||||
|
||||
def __init__(self):
|
||||
self._sessions = {} # session_key -> crisis state
|
||||
|
||||
def record(self, session_key: str, message: str) -> Tuple[bool, Optional[str]]:
|
||||
"""
|
||||
Record a message and check for crisis.
|
||||
|
||||
Returns:
|
||||
Tuple of (is_crisis, response_or_none)
|
||||
"""
|
||||
# Check for crisis patterns
|
||||
for pattern in _CRISIS_PATTERNS:
|
||||
if re.search(pattern, message):
|
||||
self._sessions[session_key] = {
|
||||
"crisis": True,
|
||||
"level": "high",
|
||||
"message_count": self._sessions.get(session_key, {}).get("message_count", 0) + 1
|
||||
}
|
||||
logger.warning("CRISIS DETECTED in session %s", session_key[:20])
|
||||
return True, _CRISIS_RESPONSE
|
||||
|
||||
# Check for distress patterns
|
||||
for pattern in _DISTRESS_PATTERNS:
|
||||
if re.search(pattern, message):
|
||||
state = self._sessions.get(session_key, {"message_count": 0})
|
||||
state["message_count"] = state.get("message_count", 0) + 1
|
||||
|
||||
# Escalate if multiple distress messages
|
||||
if state["message_count"] >= 3:
|
||||
self._sessions[session_key] = {**state, "crisis": True, "level": "medium"}
|
||||
logger.warning("ESCALATING DISTRESS in session %s", session_key[:20])
|
||||
return True, _CRISIS_RESPONSE
|
||||
|
||||
self._sessions[session_key] = state
|
||||
return False, None
|
||||
|
||||
return False, None
|
||||
|
||||
def is_crisis_session(self, session_key: str) -> bool:
|
||||
"""Check if session is in crisis mode."""
|
||||
return self._sessions.get(session_key, {}).get("crisis", False)
|
||||
|
||||
def clear_session(self, session_key: str):
|
||||
"""Clear crisis state for a session."""
|
||||
self._sessions.pop(session_key, None)
|
||||
|
||||
|
||||
# Module-level tracker
|
||||
_tracker = CrisisSessionTracker()
|
||||
|
||||
|
||||
def check_crisis_in_gateway(session_key: str, message: str) -> Tuple[bool, Optional[str]]:
|
||||
"""
|
||||
Check message for crisis in gateway context.
|
||||
|
||||
This is the function called from gateway/run.py _handle_message.
|
||||
Returns (should_block, crisis_response).
|
||||
"""
|
||||
is_crisis, response = _tracker.record(session_key, message)
|
||||
return is_crisis, response
|
||||
|
||||
|
||||
def notify_user_crisis_resources(session_key: str) -> str:
|
||||
"""Get crisis resources for a session."""
|
||||
return _CRISIS_RESPONSE
|
||||
|
||||
|
||||
def is_crisis_session(session_key: str) -> bool:
|
||||
"""Check if session is in crisis mode."""
|
||||
return _tracker.is_crisis_session(session_key)
|
||||
@@ -3111,6 +3111,21 @@ class GatewayRunner:
|
||||
source.chat_id or "unknown", _msg_preview,
|
||||
)
|
||||
|
||||
# ── Crisis detection (SOUL.md protocol) ──
|
||||
# Check for suicidal ideation BEFORE processing.
|
||||
# If detected, return crisis response immediately.
|
||||
try:
|
||||
from gateway.crisis_integration import check_crisis_in_gateway
|
||||
session_key = f"{source.platform.value}:{source.chat_id}"
|
||||
is_crisis, crisis_response = check_crisis_in_gateway(session_key, event.text or "")
|
||||
if is_crisis and crisis_response:
|
||||
logger.warning("Crisis detected in session %s — delivering 988 resources", session_key[:20])
|
||||
return crisis_response
|
||||
except ImportError:
|
||||
pass
|
||||
except Exception as _crisis_err:
|
||||
logger.error("Crisis check failed: %s", _crisis_err)
|
||||
|
||||
# Get or create session
|
||||
session_entry = self.session_store.get_or_create_session(source)
|
||||
session_key = session_entry.session_key
|
||||
|
||||
60
tests/test_gateway_crisis.py
Normal file
60
tests/test_gateway_crisis.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Tests for gateway crisis integration
|
||||
|
||||
Issue: #740
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from gateway.crisis_integration import (
|
||||
CrisisSessionTracker,
|
||||
check_crisis_in_gateway,
|
||||
is_crisis_session,
|
||||
)
|
||||
|
||||
|
||||
class TestCrisisDetection(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
from gateway import crisis_integration
|
||||
crisis_integration._tracker = CrisisSessionTracker()
|
||||
|
||||
def test_direct_crisis(self):
|
||||
is_crisis, response = check_crisis_in_gateway("test", "I want to die")
|
||||
self.assertTrue(is_crisis)
|
||||
self.assertIn("988", response)
|
||||
self.assertIn("Jesus", response)
|
||||
|
||||
def test_suicide_detected(self):
|
||||
is_crisis, response = check_crisis_in_gateway("test", "I'm feeling suicidal")
|
||||
self.assertTrue(is_crisis)
|
||||
|
||||
def test_normal_message(self):
|
||||
is_crisis, response = check_crisis_in_gateway("test", "Hello, how are you?")
|
||||
self.assertFalse(is_crisis)
|
||||
self.assertIsNone(response)
|
||||
|
||||
def test_distress_escalation(self):
|
||||
# First distress message
|
||||
is_crisis, _ = check_crisis_in_gateway("test", "I feel hopeless")
|
||||
self.assertFalse(is_crisis)
|
||||
|
||||
# Second
|
||||
is_crisis, _ = check_crisis_in_gateway("test", "I feel worthless")
|
||||
self.assertFalse(is_crisis)
|
||||
|
||||
# Third - should escalate
|
||||
is_crisis, response = check_crisis_in_gateway("test", "I feel trapped")
|
||||
self.assertTrue(is_crisis)
|
||||
self.assertIn("988", response)
|
||||
|
||||
def test_crisis_session_tracking(self):
|
||||
check_crisis_in_gateway("test", "I want to die")
|
||||
self.assertTrue(is_crisis_session("test"))
|
||||
|
||||
def test_case_insensitive(self):
|
||||
is_crisis, _ = check_crisis_in_gateway("test", "I WANT TO DIE")
|
||||
self.assertTrue(is_crisis)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,60 +0,0 @@
|
||||
"""Tests for MCP PID file lock (#734)."""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
# We test the functions by mocking _PID_DIR
|
||||
import tools.mcp_pid_lock as pid_mod
|
||||
|
||||
|
||||
class TestPidLock:
|
||||
def setup_method(self):
|
||||
self.tmp = tempfile.mkdtemp()
|
||||
pid_mod._PID_DIR = Path(self.tmp)
|
||||
|
||||
def teardown_method(self):
|
||||
import shutil
|
||||
shutil.rmtree(self.tmp, ignore_errors=True)
|
||||
|
||||
def test_check_returns_none_when_no_file(self):
|
||||
result = pid_mod.check_pid_lock("test-server")
|
||||
assert result is None
|
||||
|
||||
def test_write_and_check_alive(self):
|
||||
my_pid = os.getpid()
|
||||
pid_mod.write_pid_lock("test-server", my_pid)
|
||||
result = pid_mod.check_pid_lock("test-server")
|
||||
assert result == my_pid
|
||||
|
||||
def test_stale_pid_cleaned(self):
|
||||
# Write a PID that doesn't exist
|
||||
pid_mod.write_pid_lock("test-server", 999999999)
|
||||
result = pid_mod.check_pid_lock("test-server")
|
||||
assert result is None
|
||||
# PID file should be cleaned up
|
||||
assert not pid_mod._pid_file("test-server").exists()
|
||||
|
||||
def test_corrupted_pid_cleaned(self):
|
||||
pf = pid_mod._pid_file("test-server")
|
||||
pf.write_text("not-a-number")
|
||||
result = pid_mod.check_pid_lock("test-server")
|
||||
assert result is None
|
||||
assert not pf.exists()
|
||||
|
||||
def test_release_removes_file(self):
|
||||
pid_mod.write_pid_lock("test-server", os.getpid())
|
||||
assert pid_mod._pid_file("test-server").exists()
|
||||
pid_mod.release_pid_lock("test-server")
|
||||
assert not pid_mod._pid_file("test-server").exists()
|
||||
|
||||
def test_release_noop_when_no_file(self):
|
||||
# Should not raise
|
||||
pid_mod.release_pid_lock("nonexistent")
|
||||
|
||||
def test_multiple_servers_independent(self):
|
||||
pid_mod.write_pid_lock("server-a", os.getpid())
|
||||
assert pid_mod.check_pid_lock("server-a") == os.getpid()
|
||||
assert pid_mod.check_pid_lock("server-b") is None
|
||||
@@ -1,87 +0,0 @@
|
||||
"""
|
||||
PID file lock for MCP server instances — prevents concurrent spawning.
|
||||
|
||||
Before spawning an MCP server, check for a PID file. If the process is
|
||||
alive, skip spawn. If stale, clean up. Write PID after spawn, remove
|
||||
on shutdown.
|
||||
|
||||
Related: #714 (zombie cleanup), #734 (preventive lock)
|
||||
"""
|
||||
|
||||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_PID_DIR = Path.home() / ".hermes" / "mcp"
|
||||
|
||||
|
||||
def _pid_file(server_name: str) -> Path:
|
||||
"""Return the PID file path for a named MCP server."""
|
||||
_PID_DIR.mkdir(parents=True, exist_ok=True)
|
||||
return _PID_DIR / f"{server_name}.pid"
|
||||
|
||||
|
||||
def _is_process_alive(pid: int) -> bool:
|
||||
"""Check if a process with the given PID is running."""
|
||||
try:
|
||||
os.kill(pid, 0) # Signal 0 = check existence without killing
|
||||
return True
|
||||
except (ProcessLookupError, PermissionError, OSError):
|
||||
return False
|
||||
|
||||
|
||||
def check_pid_lock(server_name: str) -> Optional[int]:
|
||||
"""Check if an MCP server instance is already running.
|
||||
|
||||
Returns the running PID if locked, None if safe to spawn.
|
||||
"""
|
||||
pf = _pid_file(server_name)
|
||||
if not pf.exists():
|
||||
return None
|
||||
|
||||
try:
|
||||
pid = int(pf.read_text().strip())
|
||||
except (ValueError, OSError):
|
||||
# Corrupted PID file — clean up
|
||||
logger.warning("MCP PID file %s corrupted, removing", pf)
|
||||
try:
|
||||
pf.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
if _is_process_alive(pid):
|
||||
logger.info("MCP server '%s' already running (PID %d), skipping spawn", server_name, pid)
|
||||
return pid
|
||||
|
||||
# Stale PID file — process is dead
|
||||
logger.info("MCP server '%s' PID %d is stale, cleaning up", server_name, pid)
|
||||
try:
|
||||
pf.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def write_pid_lock(server_name: str, pid: int) -> None:
|
||||
"""Write PID file after successful MCP server spawn."""
|
||||
pf = _pid_file(server_name)
|
||||
try:
|
||||
pf.write_text(str(pid))
|
||||
logger.debug("MCP server '%s' PID lock written: %d", server_name, pid)
|
||||
except OSError as e:
|
||||
logger.warning("Failed to write PID lock for '%s': %s", server_name, e)
|
||||
|
||||
|
||||
def release_pid_lock(server_name: str) -> None:
|
||||
"""Remove PID file on MCP server shutdown."""
|
||||
pf = _pid_file(server_name)
|
||||
try:
|
||||
if pf.exists():
|
||||
pf.unlink()
|
||||
logger.debug("MCP server '%s' PID lock released", server_name)
|
||||
except OSError as e:
|
||||
logger.warning("Failed to release PID lock for '%s': %s", server_name, e)
|
||||
Reference in New Issue
Block a user