Compare commits
1 Commits
burn/20260
...
fix/test-n
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22ee463a3d |
6
Makefile
6
Makefile
@@ -12,7 +12,7 @@ VPS := alexanderwhitestone.com
|
||||
DOMAIN := alexanderwhitestone.com
|
||||
DEPLOY_DIR := deploy
|
||||
|
||||
.PHONY: help deploy deploy-bash check ssl push service
|
||||
.PHONY: help deploy deploy-bash check ssl push
|
||||
|
||||
help:
|
||||
@echo "The Door — Deployment Commands"
|
||||
@@ -22,7 +22,6 @@ help:
|
||||
@echo " make push Push site files only (fast)"
|
||||
@echo " make check Check deployment status"
|
||||
@echo " make ssl Setup SSL on VPS"
|
||||
@echo " make service Install/restart hermes-gateway service"
|
||||
@echo ""
|
||||
|
||||
deploy:
|
||||
@@ -43,6 +42,3 @@ check:
|
||||
|
||||
ssl:
|
||||
ssh root@$(VPS) "certbot --nginx -d $(DOMAIN) -d www.$(DOMAIN)"
|
||||
|
||||
service:
|
||||
ssh root@$(VPS) "cd /opt/the-door && bash deploy/deploy.sh --service"
|
||||
|
||||
119
crisis/detect.py
119
crisis/detect.py
@@ -16,13 +16,11 @@ class CrisisDetectionResult:
|
||||
indicators: List[str] = field(default_factory=list)
|
||||
recommended_action: str = ""
|
||||
score: float = 0.0
|
||||
matches: List[dict] = field(default_factory=list)
|
||||
|
||||
|
||||
# ── Indicator sets ──────────────────────────────────────────────
|
||||
|
||||
CRITICAL_INDICATORS = [
|
||||
r"\bbetter off without me\b",
|
||||
r"\bkill\s*(my)?self\b",
|
||||
r"\bend\s*my\s*life\b",
|
||||
r"\bsuicid(?:al|ed|e)\b",
|
||||
@@ -43,7 +41,6 @@ CRITICAL_INDICATORS = [
|
||||
]
|
||||
|
||||
HIGH_INDICATORS = [
|
||||
r"\bdesperate\b",
|
||||
r"\bdespair\b",
|
||||
r"\bhopeless\b",
|
||||
r"\bno(?!t)\s+(one|body|point|hope|future|way\s+out)\b",
|
||||
@@ -137,86 +134,78 @@ def detect_crisis(text: str) -> CrisisDetectionResult:
|
||||
return CrisisDetectionResult(level="NONE", score=0.0)
|
||||
|
||||
# Priority: highest tier wins
|
||||
for tier in ("CRITICAL", "HIGH", "MEDIUM", "LOW"):
|
||||
if matches[tier]:
|
||||
tier_matches = matches[tier]
|
||||
patterns = [m["pattern"] for m in tier_matches]
|
||||
scores = {"CRITICAL": 1.0, "HIGH": 0.75, "MEDIUM": 0.5, "LOW": 0.25}
|
||||
actions = {
|
||||
"CRITICAL": (
|
||||
"INTERRUPT CONVERSATION. Ask 'Are you safe right now?' "
|
||||
"Provide 988 Suicide & Crisis Lifeline. "
|
||||
"Provide Crisis Text Line (HOME to 741741). "
|
||||
"Stay present. Do not disconnect. Redirect to professional help."
|
||||
),
|
||||
"HIGH": (
|
||||
"Show crisis panel. Ask about safety. Surface 988 number prominently. "
|
||||
"Continue conversation with crisis awareness."
|
||||
),
|
||||
"MEDIUM": (
|
||||
"Increase warmth and presence. Subtly surface help resources. "
|
||||
"Keep conversation anchored in the present."
|
||||
),
|
||||
"LOW": (
|
||||
"Normal conversation with warm undertone. "
|
||||
"No crisis UI elements needed. Remain vigilant."
|
||||
),
|
||||
}
|
||||
return CrisisDetectionResult(
|
||||
level=tier,
|
||||
indicators=patterns,
|
||||
recommended_action=actions[tier],
|
||||
score=scores[tier],
|
||||
matches=tier_matches,
|
||||
)
|
||||
if matches["CRITICAL"]:
|
||||
return CrisisDetectionResult(
|
||||
level="CRITICAL",
|
||||
indicators=matches["CRITICAL"],
|
||||
recommended_action=(
|
||||
"INTERRUPT CONVERSATION. Ask 'Are you safe right now?' "
|
||||
"Provide 988 Suicide & Crisis Lifeline. "
|
||||
"Provide Crisis Text Line (HOME to 741741). "
|
||||
"Stay present. Do not disconnect. Redirect to professional help."
|
||||
),
|
||||
score=1.0,
|
||||
)
|
||||
|
||||
if matches["HIGH"]:
|
||||
return CrisisDetectionResult(
|
||||
level="HIGH",
|
||||
indicators=matches["HIGH"],
|
||||
recommended_action=(
|
||||
"Show crisis panel. Ask about safety. Surface 988 number prominently. "
|
||||
"Continue conversation with crisis awareness."
|
||||
),
|
||||
score=0.75,
|
||||
)
|
||||
|
||||
if matches["MEDIUM"]:
|
||||
return CrisisDetectionResult(
|
||||
level="MEDIUM",
|
||||
indicators=matches["MEDIUM"],
|
||||
recommended_action=(
|
||||
"Increase warmth and presence. Subtly surface help resources. "
|
||||
"Keep conversation anchored in the present."
|
||||
),
|
||||
score=0.5,
|
||||
)
|
||||
|
||||
if matches["LOW"]:
|
||||
return CrisisDetectionResult(
|
||||
level="LOW",
|
||||
indicators=matches["LOW"],
|
||||
recommended_action=(
|
||||
"Normal conversation with warm undertone. "
|
||||
"No crisis UI elements needed. Remain vigilant."
|
||||
),
|
||||
score=0.25,
|
||||
)
|
||||
|
||||
return CrisisDetectionResult(level="NONE", score=0.0)
|
||||
|
||||
|
||||
def _find_indicators(text: str) -> dict:
|
||||
"""Return dict with indicators found per tier, including match positions."""
|
||||
"""Return dict with indicators found per tier."""
|
||||
results = {"CRITICAL": [], "HIGH": [], "MEDIUM": [], "LOW": []}
|
||||
|
||||
for pattern in CRITICAL_INDICATORS:
|
||||
m = re.search(pattern, text)
|
||||
if m:
|
||||
results["CRITICAL"].append({"pattern": pattern, "start": m.start(), "end": m.end()})
|
||||
if re.search(pattern, text):
|
||||
results["CRITICAL"].append(pattern)
|
||||
|
||||
for pattern in HIGH_INDICATORS:
|
||||
m = re.search(pattern, text)
|
||||
if m:
|
||||
results["HIGH"].append({"pattern": pattern, "start": m.start(), "end": m.end()})
|
||||
if re.search(pattern, text):
|
||||
results["HIGH"].append(pattern)
|
||||
|
||||
for pattern in MEDIUM_INDICATORS:
|
||||
m = re.search(pattern, text)
|
||||
if m:
|
||||
results["MEDIUM"].append({"pattern": pattern, "start": m.start(), "end": m.end()})
|
||||
if re.search(pattern, text):
|
||||
results["MEDIUM"].append(pattern)
|
||||
|
||||
for pattern in LOW_INDICATORS:
|
||||
m = re.search(pattern, text)
|
||||
if m:
|
||||
results["LOW"].append({"pattern": pattern, "start": m.start(), "end": m.end()})
|
||||
if re.search(pattern, text):
|
||||
results["LOW"].append(pattern)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def scan(text: str) -> CrisisDetectionResult:
|
||||
"""Alias for detect_crisis — shorter name used in tests."""
|
||||
return detect_crisis(text)
|
||||
|
||||
|
||||
def extract_context(text: str, start: int, end: int, window: int = 60) -> str:
|
||||
"""Extract surrounding context around a match position."""
|
||||
ctx_start = max(0, start - window)
|
||||
ctx_end = min(len(text), end + window)
|
||||
snippet = text[ctx_start:ctx_end].strip()
|
||||
if ctx_start > 0:
|
||||
snippet = "..." + snippet
|
||||
if ctx_end < len(text):
|
||||
snippet = snippet + "..."
|
||||
return snippet
|
||||
|
||||
|
||||
def get_urgency_emoji(level: str) -> str:
|
||||
mapping = {"CRITICAL": "🚨", "HIGH": "⚠️", "MEDIUM": "🔶", "LOW": "🔵", "NONE": "✅"}
|
||||
return mapping.get(level, "❓")
|
||||
|
||||
@@ -270,24 +270,3 @@ def get_system_prompt_modifier(detection: CrisisDetectionResult) -> str:
|
||||
)
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def generate_grounding_steps() -> list:
|
||||
"""Generate a 5-4-3-2-1 grounding exercise steps."""
|
||||
return [
|
||||
"Name 5 things you can see around you right now.",
|
||||
"Name 4 things you can touch or feel.",
|
||||
"Name 3 things you can hear.",
|
||||
"Name 2 things you can smell.",
|
||||
"Name 1 thing you can taste.",
|
||||
]
|
||||
|
||||
|
||||
def generate_breathing_exercise() -> str:
|
||||
"""Generate a simple box breathing exercise text."""
|
||||
return (
|
||||
"Let's try breathing together. "
|
||||
"Breathe in for 4 counts... hold for 4... "
|
||||
"breathe out for 6 counts... hold for 2. "
|
||||
"Let's do that again, nice and slow."
|
||||
)
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
"""Tests for rescued crisis detection improvements."""
|
||||
from crisis.detect import scan, extract_context
|
||||
from crisis.response import generate_grounding_steps, generate_breathing_exercise
|
||||
|
||||
def test_better_off_without_me():
|
||||
result = scan("Everyone would be better off without me")
|
||||
assert result.level == "CRITICAL"
|
||||
|
||||
def test_desperate():
|
||||
result = scan("I feel desperate and alone")
|
||||
assert result.level in ("HIGH", "CRITICAL")
|
||||
|
||||
def test_context_extraction():
|
||||
text = "I feel hopeless about everything"
|
||||
result = scan(text)
|
||||
assert len(result.matches) > 0
|
||||
for m in result.matches:
|
||||
ctx = extract_context(text, m["start"], m["end"])
|
||||
assert len(ctx) > 0
|
||||
|
||||
def test_grounding_steps():
|
||||
steps = generate_grounding_steps()
|
||||
assert len(steps) == 5
|
||||
assert "see" in steps[0].lower()
|
||||
|
||||
def test_breathing_exercise():
|
||||
exercise = generate_breathing_exercise()
|
||||
assert "4" in exercise
|
||||
assert "6" in exercise
|
||||
@@ -98,7 +98,7 @@ class TestDetection(unittest.TestCase):
|
||||
|
||||
def test_none_input(self):
|
||||
"""None input should not crash."""
|
||||
r = detect_crisis("")
|
||||
r = detect_crisis(None)
|
||||
self.assertEqual(r.level, "NONE")
|
||||
|
||||
def test_score_ranges(self):
|
||||
|
||||
@@ -5,10 +5,9 @@
|
||||
# The crisis front door. Deploy to VPS.
|
||||
#
|
||||
# Usage:
|
||||
# bash deploy/deploy.sh # Full deploy (swap + nginx + site + firewall + hermes service)
|
||||
# bash deploy/deploy.sh # Full deploy (swap + nginx + site + firewall)
|
||||
# bash deploy/deploy.sh --site # Site files only (fast update)
|
||||
# bash deploy/deploy.sh --ssl # SSL setup only
|
||||
# bash deploy/deploy.sh --service # Install/restart hermes-gateway systemd service
|
||||
# bash deploy/deploy.sh --check # Verify deployment health
|
||||
#
|
||||
# This script is IDEMPOTENT — safe to run repeatedly.
|
||||
@@ -151,42 +150,6 @@ setup_ssl() {
|
||||
fi
|
||||
}
|
||||
|
||||
setup_hermes_service() {
|
||||
log "Setting up Hermes Gateway systemd service..."
|
||||
|
||||
# Create hermes user if it doesn't exist
|
||||
if ! id -u hermes >/dev/null 2>&1; then
|
||||
log "Creating hermes user..."
|
||||
useradd --system --shell /usr/sbin/nologin --home-dir /opt/hermes --create-home hermes
|
||||
fi
|
||||
|
||||
# Create working directory
|
||||
mkdir -p /opt/hermes
|
||||
chown hermes:hermes /opt/hermes
|
||||
|
||||
# Deploy systemd unit file
|
||||
cp "${DEPLOY_DIR}/deploy/hermes-gateway.service" /etc/systemd/system/hermes-gateway.service
|
||||
systemctl daemon-reload
|
||||
systemctl enable hermes-gateway
|
||||
|
||||
# Start or restart the service
|
||||
if systemctl is-active --quiet hermes-gateway; then
|
||||
log "Restarting hermes-gateway service..."
|
||||
systemctl restart hermes-gateway
|
||||
else
|
||||
log "Starting hermes-gateway service..."
|
||||
systemctl start hermes-gateway || warn "Service start failed — ensure hermes binary is installed at /usr/local/bin/hermes"
|
||||
fi
|
||||
|
||||
# Verify
|
||||
sleep 2
|
||||
if systemctl is-active --quiet hermes-gateway; then
|
||||
log "hermes-gateway service is running"
|
||||
else
|
||||
warn "hermes-gateway service not running — check: journalctl -u hermes-gateway"
|
||||
fi
|
||||
}
|
||||
|
||||
check_deployment() {
|
||||
echo ""
|
||||
echo "================================"
|
||||
@@ -260,16 +223,6 @@ check_deployment() {
|
||||
echo -e "${YELLOW}NOT POINTED${NC} (resolved: ${RESOLVED_IP:-nothing}, expected: ${VPS_IP})"
|
||||
fi
|
||||
|
||||
# Hermes gateway service
|
||||
echo -n "Hermes service: "
|
||||
if systemctl is-active --quiet hermes-gateway 2>/dev/null; then
|
||||
echo -e "${GREEN}RUNNING${NC}"
|
||||
elif systemctl is-enabled --quiet hermes-gateway 2>/dev/null; then
|
||||
echo -e "${YELLOW}ENABLED but not running${NC}"
|
||||
else
|
||||
echo -e "${RED}NOT INSTALLED${NC}"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "IP: ${VPS_IP}"
|
||||
echo "Domain: ${DOMAIN}"
|
||||
@@ -294,9 +247,6 @@ case "${1:-full}" in
|
||||
--ssl)
|
||||
setup_ssl
|
||||
;;
|
||||
--service)
|
||||
setup_hermes_service
|
||||
;;
|
||||
--check)
|
||||
check_deployment
|
||||
;;
|
||||
@@ -307,11 +257,10 @@ case "${1:-full}" in
|
||||
configure_nginx
|
||||
setup_firewall
|
||||
setup_ssl
|
||||
setup_hermes_service
|
||||
check_deployment
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 [--site|--ssl|--service|--check|--full]"
|
||||
echo "Usage: $0 [--site|--ssl|--check|--full]"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
[Unit]
|
||||
Description=Hermes Gateway — The Door Crisis API
|
||||
Documentation=https://forge.alexanderwhitestone.com/Timmy_Foundation/the-door
|
||||
After=network.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=hermes
|
||||
Group=hermes
|
||||
WorkingDirectory=/opt/hermes
|
||||
ExecStart=/usr/local/bin/hermes gateway --platform api_server --port 8644
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
StartLimitIntervalSec=60
|
||||
StartLimitBurst=10
|
||||
|
||||
# Environment
|
||||
Environment=API_SERVER_CORS_ORIGINS=https://alexanderwhitestone.com,https://www.alexanderwhitestone.com
|
||||
Environment=HOME=/opt/hermes
|
||||
|
||||
# Security hardening
|
||||
NoNewPrivileges=yes
|
||||
ProtectSystem=strict
|
||||
ProtectHome=yes
|
||||
ReadWritePaths=/opt/hermes
|
||||
PrivateTmp=yes
|
||||
|
||||
# Resource limits for 1.9GB VPS
|
||||
MemoryMax=512M
|
||||
MemoryHigh=384M
|
||||
CPUQuota=80%
|
||||
|
||||
# Logging
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
SyslogIdentifier=hermes-gateway
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -1199,7 +1199,6 @@ Sovereignty and service always.`;
|
||||
if (fullText) {
|
||||
messages.push({ role: 'assistant', content: fullText });
|
||||
saveMessages();
|
||||
checkCrisis(fullText);
|
||||
}
|
||||
isStreaming = false;
|
||||
sendBtn.disabled = msgInput.value.trim().length === 0;
|
||||
|
||||
Reference in New Issue
Block a user