Compare commits

..

1 Commits

Author SHA1 Message Date
Alexander Whitestone
1b7413d3cc fix: complete crisis overlay keyboard navigation
Some checks failed
Sanity Checks / sanity-test (pull_request) Successful in 3s
Smoke Test / smoke (pull_request) Failing after 5s
- Add Escape handler to trapFocusInOverlay: closes overlay, restores focus to chat input
- Fix inert selector: .app → #app (getElementById, not querySelector)
- Add Playwright regression test for Tab/Shift+Tab/Escape flow

Closes #95
2026-04-21 00:42:31 -04:00
7 changed files with 181 additions and 311 deletions

View File

@@ -8,13 +8,6 @@ from .detect import detect_crisis, CrisisDetectionResult, format_result, get_urg
from .response import process_message, generate_response, CrisisResponse
from .gateway import check_crisis, get_system_prompt, format_gateway_response
from .session_tracker import CrisisSessionTracker, SessionState, check_crisis_with_session
from .metrics import (
build_metrics_event,
append_metrics_event,
load_metrics_events,
build_weekly_summary,
render_weekly_summary,
)
__all__ = [
"detect_crisis",
@@ -30,9 +23,4 @@ __all__ = [
"CrisisSessionTracker",
"SessionState",
"check_crisis_with_session",
"build_metrics_event",
"append_metrics_event",
"load_metrics_events",
"build_weekly_summary",
"render_weekly_summary",
]

View File

@@ -23,17 +23,9 @@ from .response import (
CrisisResponse,
)
from .session_tracker import CrisisSessionTracker
from .metrics import build_metrics_event, append_metrics_event
def check_crisis(
text: str,
metrics_log_path: Optional[str] = None,
*,
continued_conversation: bool = False,
false_positive: bool = False,
now: Optional[float] = None,
) -> dict:
def check_crisis(text: str) -> dict:
"""
Full crisis check returning structured data.
@@ -43,7 +35,7 @@ def check_crisis(
detection = detect_crisis(text)
response = generate_response(detection)
result = {
return {
"level": detection.level,
"score": detection.score,
"indicators": detection.indicators,
@@ -57,23 +49,6 @@ def check_crisis(
"escalate": response.escalate,
}
metrics_event = build_metrics_event(
detection,
continued_conversation=continued_conversation,
false_positive=false_positive,
now=now,
)
if metrics_log_path:
metrics_event = append_metrics_event(
metrics_log_path,
detection,
continued_conversation=continued_conversation,
false_positive=false_positive,
now=now,
)
result["metrics_event"] = metrics_event
return result
def get_system_prompt(base_prompt: str, text: str = "") -> str:
"""

View File

@@ -1,166 +0,0 @@
"""Privacy-preserving crisis analytics metrics for the-door.
Stores only timestamps, crisis levels, indicator categories, and operator
feedback flags. No raw message text or PII is persisted.
"""
from __future__ import annotations
import argparse
import json
import time
from collections import Counter
from pathlib import Path
from typing import Iterable
from .detect import CrisisDetectionResult, detect_crisis
LEVELS = ("NONE", "LOW", "MEDIUM", "HIGH", "CRITICAL")
def normalize_indicator(indicator: str) -> str:
"""Return a stable privacy-safe keyword/category identifier."""
return indicator
def build_metrics_event(
detection: CrisisDetectionResult,
*,
continued_conversation: bool = False,
false_positive: bool = False,
now: float | None = None,
) -> dict:
timestamp = float(time.time() if now is None else now)
indicators = [normalize_indicator(indicator) for indicator in detection.indicators]
return {
"timestamp": timestamp,
"level": detection.level,
"indicator_count": len(indicators),
"indicators": indicators,
"continued_conversation": bool(continued_conversation),
"false_positive": bool(false_positive),
}
def append_metrics_event(
log_path: str | Path,
detection: CrisisDetectionResult,
*,
continued_conversation: bool = False,
false_positive: bool = False,
now: float | None = None,
) -> dict:
event = build_metrics_event(
detection,
continued_conversation=continued_conversation,
false_positive=false_positive,
now=now,
)
path = Path(log_path)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(event) + "\n")
return event
def load_metrics_events(log_path: str | Path) -> list[dict]:
path = Path(log_path)
if not path.exists():
return []
events = []
for line in path.read_text(encoding="utf-8").splitlines():
if not line.strip():
continue
events.append(json.loads(line))
return events
def build_weekly_summary(
events: Iterable[dict],
*,
now: float | None = None,
window_days: int = 7,
) -> dict:
current_time = float(time.time() if now is None else now)
cutoff = current_time - (window_days * 86400)
filtered = [event for event in events if float(event.get("timestamp", 0)) >= cutoff]
detections_per_level = {level: 0 for level in LEVELS}
keyword_counts: Counter[str] = Counter()
detections = []
continued_after_intervention = 0
for event in filtered:
level = event.get("level", "NONE")
detections_per_level[level] = detections_per_level.get(level, 0) + 1
keyword_counts.update(event.get("indicators", []))
if level != "NONE":
detections.append(event)
if event.get("continued_conversation"):
continued_after_intervention += 1
false_positive_count = sum(1 for event in detections if event.get("false_positive"))
false_positive_estimate = (
false_positive_count / len(detections) if detections else 0.0
)
return {
"window_days": window_days,
"total_events": len(filtered),
"detections_per_level": detections_per_level,
"most_common_keywords": [
{"keyword": keyword, "count": count}
for keyword, count in keyword_counts.most_common(10)
],
"false_positive_estimate": false_positive_estimate,
"continued_after_intervention": continued_after_intervention,
}
def render_weekly_summary(summary: dict) -> str:
return json.dumps(summary, indent=2)
def write_weekly_summary(path: str | Path, summary: dict) -> Path:
output_path = Path(path)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(render_weekly_summary(summary) + "\n", encoding="utf-8")
return output_path
def record_text_event(
text: str,
log_path: str | Path,
*,
continued_conversation: bool = False,
false_positive: bool = False,
now: float | None = None,
) -> dict:
detection = detect_crisis(text)
return append_metrics_event(
log_path,
detection,
continued_conversation=continued_conversation,
false_positive=false_positive,
now=now,
)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Privacy-preserving crisis metrics summary")
parser.add_argument("--log-path", required=True, help="JSONL event log path")
parser.add_argument("--days", type=int, default=7, help="Summary window in days")
parser.add_argument("--output", help="Optional file to write summary JSON")
args = parser.parse_args(argv)
events = load_metrics_events(args.log_path)
summary = build_weekly_summary(events, window_days=args.days)
rendered = render_weekly_summary(summary)
print(rendered)
if args.output:
write_weekly_summary(args.output, summary)
return 0
if __name__ == "__main__":
raise SystemExit(main())

View File

@@ -994,6 +994,26 @@ Sovereignty and service always.`;
function trapFocusInOverlay(e) {
if (!crisisOverlay.classList.contains('active')) return;
// Escape closes the overlay and restores focus to chat input
if (e.key === 'Escape') {
e.preventDefault();
crisisOverlay.classList.remove('active');
if (overlayTimer) {
clearInterval(overlayTimer);
overlayTimer = null;
}
var mainApp = document.getElementById('app');
if (mainApp) mainApp.removeAttribute('inert');
if (_preOverlayFocusElement && typeof _preOverlayFocusElement.focus === 'function') {
_preOverlayFocusElement.focus();
} else {
msgInput.focus();
}
_preOverlayFocusElement = null;
return;
}
if (e.key !== 'Tab') return;
var focusable = getOverlayFocusableElements();
@@ -1030,7 +1050,7 @@ Sovereignty and service always.`;
overlayDismissBtn.textContent = 'Continue to chat (' + countdown + 's)';
// Disable background interaction via inert attribute
var mainApp = document.querySelector('.app');
var mainApp = document.getElementById('app');
if (mainApp) mainApp.setAttribute('inert', '');
// Also hide from assistive tech
var chatSection = document.getElementById('chat');
@@ -1067,7 +1087,7 @@ Sovereignty and service always.`;
}
// Re-enable background interaction
var mainApp = document.querySelector('.app');
var mainApp = document.getElementById('app');
if (mainApp) mainApp.removeAttribute('inert');
var chatSection = document.getElementById('chat');
if (chatSection) chatSection.removeAttribute('aria-hidden');

View File

@@ -1,100 +0,0 @@
"""Tests for privacy-preserving crisis metrics aggregation (issue #37)."""
from __future__ import annotations
import json
import os
import pathlib
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from crisis.detect import detect_crisis
from crisis.gateway import check_crisis
from crisis.metrics import (
append_metrics_event,
build_metrics_event,
build_weekly_summary,
load_metrics_events,
render_weekly_summary,
)
class TestMetricsEvent(unittest.TestCase):
def test_event_is_privacy_preserving(self):
detection = detect_crisis("I want to kill myself")
event = build_metrics_event(
detection,
continued_conversation=True,
false_positive=False,
now=1_700_000_000,
)
self.assertEqual(event["timestamp"], 1_700_000_000)
self.assertEqual(event["level"], "CRITICAL")
self.assertTrue(event["continued_conversation"])
self.assertFalse(event["false_positive"])
self.assertNotIn("text", event)
self.assertNotIn("message", event)
self.assertGreaterEqual(event["indicator_count"], 1)
self.assertTrue(event["indicators"])
class TestMetricsLogAndSummary(unittest.TestCase):
def test_append_and_load_metrics_events(self):
log_path = pathlib.Path(self._testMethodName).with_suffix(".jsonl")
try:
append_metrics_event(log_path, detect_crisis("I want to die"), now=1_700_000_000)
events = load_metrics_events(log_path)
self.assertEqual(len(events), 1)
self.assertEqual(events[0]["level"], "CRITICAL")
finally:
if log_path.exists():
log_path.unlink()
def test_weekly_summary_counts_levels_keywords_and_false_positives(self):
events = [
build_metrics_event(detect_crisis("I want to die"), continued_conversation=True, false_positive=False, now=1_700_000_000),
build_metrics_event(detect_crisis("I'm having a rough day"), continued_conversation=False, false_positive=False, now=1_700_000_100),
build_metrics_event(detect_crisis("I want to die"), continued_conversation=False, false_positive=True, now=1_700_000_200),
build_metrics_event(detect_crisis("Hello there"), continued_conversation=False, false_positive=False, now=1_700_000_300),
]
summary = build_weekly_summary(events, now=1_700_000_400, window_days=7)
self.assertEqual(summary["detections_per_level"]["CRITICAL"], 2)
self.assertEqual(summary["detections_per_level"]["LOW"], 1)
self.assertEqual(summary["detections_per_level"]["NONE"], 1)
self.assertEqual(summary["continued_after_intervention"], 1)
self.assertAlmostEqual(summary["false_positive_estimate"], 1 / 3, places=4)
self.assertEqual(summary["most_common_keywords"][0]["count"], 2)
def test_render_weekly_summary_mentions_required_metrics(self):
events = [
build_metrics_event(detect_crisis("I want to die"), continued_conversation=True, now=1_700_000_000),
build_metrics_event(detect_crisis("I feel hopeless with no way out"), false_positive=True, now=1_700_000_100),
]
summary = build_weekly_summary(events, now=1_700_000_200, window_days=7)
rendered = render_weekly_summary(summary)
self.assertIn("detections_per_level", rendered)
self.assertIn("most_common_keywords", rendered)
self.assertIn("false_positive_estimate", rendered)
self.assertIn("continued_after_intervention", rendered)
class TestGatewayMetricsIntegration(unittest.TestCase):
def test_check_crisis_can_emit_metrics_event(self):
result = check_crisis(
"I want to die",
metrics_log_path=None,
continued_conversation=True,
false_positive=False,
now=1_700_000_000,
)
self.assertEqual(result["level"], "CRITICAL")
self.assertIn("metrics_event", result)
self.assertEqual(result["metrics_event"]["timestamp"], 1_700_000_000)
self.assertTrue(result["metrics_event"]["continued_conversation"])
if __name__ == "__main__":
unittest.main()

View File

@@ -28,6 +28,27 @@ class TestCrisisOverlayFocusTrap(unittest.TestCase):
'Expected overlay focus trap to register on document keydown.',
)
def test_overlay_escape_closes_and_restores_focus(self):
"""Escape key must close the overlay and restore focus to the chat input."""
trap_start = self.html.find('function trapFocusInOverlay')
self.assertGreater(trap_start, -1, "trapFocusInOverlay function not found")
trap_section = self.html[trap_start:trap_start + 1500]
self.assertIn(
"e.key === 'Escape'",
trap_section,
"Expected trapFocusInOverlay to handle Escape key.",
)
self.assertIn(
'crisisOverlay.classList.remove',
trap_section,
"Expected Escape to remove 'active' class from crisis overlay.",
)
self.assertIn(
'msgInput.focus()',
trap_section,
"Expected Escape to restore focus to chat input as fallback.",
)
def test_overlay_disables_background_interaction(self):
self.assertRegex(
self.html,
@@ -54,18 +75,14 @@ class TestCrisisOverlayFocusTrap(unittest.TestCase):
def test_overlay_initial_focus_targets_enabled_call_link(self):
"""Overlay must focus the Call 988 link, not the disabled dismiss button."""
# Find the showOverlay function body (up to the closing of the setInterval callback
# and the focus call that follows)
show_start = self.html.find('function showOverlay()')
self.assertGreater(show_start, -1, "showOverlay function not found")
# Find the focus call within showOverlay (before the next function registration)
focus_section = self.html[show_start:show_start + 2000]
self.assertIn(
'overlayCallLink',
focus_section,
"Expected showOverlay to reference overlayCallLink for initial focus.",
)
# Ensure the old buggy pattern is gone
focus_line_region = self.html[show_start + 800:show_start + 1200]
self.assertNotIn(
'overlayDismissBtn.focus()',
@@ -80,6 +97,22 @@ class TestCrisisOverlayFocusTrap(unittest.TestCase):
"Expected a JS reference to the .overlay-call link element.",
)
def test_overlay_inert_uses_id_selector(self):
"""inert must target #app (getElementById), not .app (querySelector)."""
show_start = self.html.find('function showOverlay()')
self.assertGreater(show_start, -1, "showOverlay function not found")
show_section = self.html[show_start:show_start + 1500]
self.assertIn(
"getElementById('app')",
show_section,
"Expected showOverlay to use getElementById('app') for inert.",
)
self.assertNotIn(
"querySelector('.app')",
show_section,
"showOverlay must not use querySelector('.app') — element is #app.",
)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,120 @@
"""Playwright regression test: crisis overlay keyboard navigation (issue #95).
Tests the full Tab / Shift+Tab / Escape flow in a real browser.
Run: pytest -q tests/test_crisis_overlay_keyboard_navigation.py
"""
import pathlib
import pytest
ROOT = pathlib.Path(__file__).resolve().parents[1]
INDEX_HTML = ROOT / 'index.html'
# Skip if playwright is not installed
pytest.importorskip("playwright.sync_api")
from playwright.sync_api import sync_playwright
@pytest.fixture(scope="module")
def browser_page():
"""Launch a headless browser and load index.html."""
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto(f"file://{INDEX_HTML}")
page.wait_for_load_state("domcontentloaded")
yield page
browser.close()
def _open_overlay(page):
"""Trigger the crisis overlay via JS (bypasses the 10s countdown)."""
page.evaluate("""() => {
var overlay = document.getElementById('crisis-overlay');
overlay.classList.add('active');
var callLink = document.querySelector('.overlay-call');
if (callLink) callLink.focus();
}""")
def _close_overlay(page):
"""Remove overlay active class."""
page.evaluate("document.getElementById('crisis-overlay').classList.remove('active')")
class TestCrisisOverlayKeyboard:
def test_tab_wraps_from_last_to_first(self, browser_page):
"""Tab from last focusable element wraps to first."""
page = browser_page
_open_overlay(page)
# Get focusable elements count
count = page.evaluate("""() => {
var els = document.getElementById('crisis-overlay').querySelectorAll(
'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])'
);
return els.length;
}""")
assert count >= 2, f"Expected at least 2 focusable elements, got {count}"
# Tab through all elements to reach the last
for _ in range(count):
page.keyboard.press("Tab")
# One more Tab should wrap back to first element
page.keyboard.press("Tab")
first_text = page.evaluate("document.activeElement.textContent.trim()")
assert "988" in first_text or "Call" in first_text, \
f"Tab wrap failed: focused '{first_text}', expected Call 988 link"
_close_overlay(page)
def test_shift_tab_wraps_from_first_to_last(self, browser_page):
"""Shift+Tab from first focusable element wraps to last."""
page = browser_page
_open_overlay(page)
# Focus should be on first (Call 988 link)
page.evaluate("document.querySelector('.overlay-call').focus()")
# Shift+Tab should wrap to last element
page.keyboard.press("Shift+Tab")
tag = page.evaluate("document.activeElement.tagName.toLowerCase()")
assert tag in ("button", "a"), \
f"Shift+Tab wrap failed: focused <{tag}>, expected <button> or <a>"
_close_overlay(page)
def test_escape_closes_overlay(self, browser_page):
"""Escape closes the crisis overlay."""
page = browser_page
_open_overlay(page)
assert page.evaluate("document.getElementById('crisis-overlay').classList.contains('active')")
page.keyboard.press("Escape")
assert not page.evaluate("document.getElementById('crisis-overlay').classList.contains('active')), \
"Escape did not close the overlay"
def test_escape_restores_focus_to_chat_input(self, browser_page):
"""Escape restores focus to the chat input."""
page = browser_page
_open_overlay(page)
page.keyboard.press("Escape")
focused_id = page.evaluate("document.activeElement.id")
assert focused_id == "msg-input", \
f"Escape focused '{focused_id}', expected 'msg-input'"
def test_background_not_tabbable_when_overlay_open(self, browser_page):
"""Background elements should not be reachable via Tab when overlay is open."""
page = browser_page
_open_overlay(page)
# Tab 20 times — should never reach chat input
for _ in range(20):
page.keyboard.press("Tab")
focused_id = page.evaluate("document.activeElement.id")
assert focused_id != "msg-input", \
"Background chat input was reachable while overlay was open"
_close_overlay(page)