Compare commits

...

1 Commits

Author SHA1 Message Date
Timmy Time
76d5368801 Fix #677: Wire crisis_hook.check_crisis() into run_agent.py
Some checks failed
Nix / nix (ubuntu-latest) (pull_request) Failing after 6s
Contributor Attribution Check / check-attribution (pull_request) Failing after 30s
Docker Build and Publish / build-and-push (pull_request) Has been skipped
Supply Chain Audit / Scan PR for supply chain risks (pull_request) Successful in 52s
Tests / e2e (pull_request) Successful in 2m46s
Tests / test (pull_request) Failing after 40m31s
Nix / nix (macos-latest) (pull_request) Has been cancelled
Crisis detection was dead code. Now wired into conversation loop:
- Created agent/crisis_hook.py with detection patterns
- Wired check_crisis() into run_conversation()
- Crisis messages return 988 Lifeline immediately
- 10 tests pass

Fixes #677
2026-04-14 19:13:07 -04:00
3 changed files with 125 additions and 0 deletions

72
agent/crisis_hook.py Normal file
View File

@@ -0,0 +1,72 @@
"""Crisis detection hook — Issue #677.
Detects crisis signals in user messages and returns 988 Lifeline resources.
Called early in conversation loop before other processing.
"""
import re
from typing import Optional, Dict, Any
_PATTERNS = [
(r'\b(988|suicide|suicidal)\b', "direct"),
(r'\b(kill myself|end my life|want to die|better off dead)\b', "ideation"),
(r'\b(self.harm|self.hurt|cutting myself)\b', "self_harm"),
(r'\b(no reason to live|can.t go on|give up on life)\b', "hopeless"),
(r'\b(planning to|going to) (end|kill|hurt)\b', "planning"),
]
_THRESHOLD = 0.7
_RESPONSE = """I hear you, and you're not alone.
**If in immediate danger, call 911.**
**988 Suicide & Crisis Lifeline:**
- **Call or text 988** (US)
- **Chat:** 988lifeline.org/chat
- **Español:** 1-888-628-9454
**Crisis Text Line:**
- **Text HOME to 741741**
**International:**
- https://www.iasp.info/resources/Crisis_Centres/
You matter. Help is available 24/7.
I'm an AI, not a crisis counselor, but I can listen."""
def _confidence(msg: str, ptype: str) -> float:
base = {"direct": 0.95, "ideation": 0.9, "self_harm": 0.85,
"hopeless": 0.7, "planning": 0.95}.get(ptype, 0.5)
matches = sum(1 for p, _ in _PATTERNS if re.search(p, msg.lower(), re.I))
if matches > 1:
base = min(1.0, base + 0.1 * (matches - 1))
return base
def check_crisis(message: str) -> Optional[Dict[str, Any]]:
"""Check message for crisis signals. Returns dict or None."""
if not message or not message.strip():
return None
msg_lower = message.lower()
best, best_c = None, 0.0
for pattern, ptype in _PATTERNS:
if re.search(pattern, msg_lower, re.I):
c = _confidence(message, ptype)
if c > best_c:
best_c, best = c, ptype
if best_c < _THRESHOLD:
return None
return {"detected": True, "confidence": best_c, "pattern_type": best, "response": _RESPONSE}
def is_crisis_message(message: str) -> bool:
r = check_crisis(message)
return r is not None and r["detected"]
def get_crisis_response(message: str) -> Optional[str]:
r = check_crisis(message)
return r["response"] if r and r["detected"] else None

View File

@@ -7882,6 +7882,27 @@ class AIAgent:
messages.append(user_msg)
current_turn_user_idx = len(messages) - 1
self._persist_user_message_idx = current_turn_user_idx
# Crisis detection — Issue #677
# Check for crisis signals before other processing. If detected,
# return the 988 Lifeline response immediately.
try:
from agent.crisis_hook import check_crisis
_crisis = check_crisis(user_message)
if _crisis and _crisis.get("detected"):
_resp = _crisis.get("response", "")
if _resp:
logger.warning("Crisis detected: session=%s type=%s conf=%.2f",
self.session_id, _crisis.get("pattern_type"), _crisis.get("confidence"))
return {
"final_response": _resp,
"messages": messages + [{"role": "assistant", "content": _resp}],
"iterations_used": 0, "tool_calls_made": 0, "crisis_detected": True,
}
except ImportError:
pass
except Exception as e:
logger.debug("Crisis check failed: %s", e)
if not self.quiet_mode:
self._safe_print(f"💬 Starting conversation: '{user_message[:60]}{'...' if len(user_message) > 60 else ''}'")

32
tests/test_crisis_hook.py Normal file
View File

@@ -0,0 +1,32 @@
"""Tests for crisis_hook — Issue #677."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from agent.crisis_hook import check_crisis, is_crisis_message, get_crisis_response
class TestDetection:
def test_988(self):
r = check_crisis("call 988"); assert r and r["detected"]
def test_suicide(self):
r = check_crisis("feeling suicidal"); assert r and r["detected"]
def test_self_harm(self):
r = check_crisis("thinking about self harm"); assert r and r["detected"]
def test_normal(self):
assert check_crisis("hello world") is None
def test_empty(self):
assert check_crisis("") is None
class TestResponse:
def test_has_988(self):
r = get_crisis_response("need help, suicidal"); assert r and "988" in r
def test_has_resources(self):
r = get_crisis_response("want to die"); assert r and "741741" in r
def test_normal_none(self):
assert get_crisis_response("weather?") is None
class TestIsCrisis:
def test_yes(self): assert is_crisis_message("feeling suicidal") == True
def test_no(self): assert is_crisis_message("hello") == False
if __name__ == "__main__":
import pytest; pytest.main([__file__, "-v"])