fix: chat replies in any situation — no false crisis re-fire, answers in crisis, medicine questions anywhere
CI / ruff · pytest (pull_request) Successful in 2m22s

- An acked crisis is not raised again for the same signal: re-raised only if a newer triggering scale exists
  (seen live: AUDIT-C 0/0/0 re-fired the acked PHQ-9 item 9 crisis).
- In an open crisis every non-acute message gets a reply (agent for questions, support otherwise) plus
  emergency contacts; acute messages still get the fixed script.
- Questions and medicine mentions without «?» are answered mid-questionnaire; free-text steps (check-in,
  side effects, question states) keep parsing their input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-04 18:23:03 +03:00
co-authored by Claude Opus 5.5
parent a026a8cd48
commit dd67905626
6 changed files with 118 additions and 7 deletions
+32
View File
@@ -0,0 +1,32 @@
"""An acked crisis is not raised again for the same signal; a newer PHQ-9 with item 9 raises a new one."""
from datetime import datetime, timedelta
from app.core.rules import Action
from app.core.types import AlertLevel
from app.services import monitoring
class _A:
def __init__(self, code, ts, scale=None):
self.code, self.ts, self.scale = code, ts, scale
async def test_drop_handled_crises(monkeypatch):
t0 = datetime(2026, 10, 4, 16, 32)
alerts = [_A("phq9_item9", t0 + timedelta(seconds=1))]
results = [_A(None, t0, "phq9"), _A(None, t0 + timedelta(hours=2), "auditc")]
async def list_alerts(s, token):
return alerts
async def list_scale_results(s, token):
return results
monkeypatch.setattr(monitoring.repo, "list_alerts", list_alerts)
monkeypatch.setattr(monitoring.repo, "list_scale_results", list_scale_results)
crisis = Action(type="crisis", level=AlertLevel.ACUTE, code="phq9_item9")
other = Action(type="alert", level=AlertLevel.HIGH, code="phq9_severe")
assert await monitoring._drop_handled_crises(None, "t", [crisis, other]) == [other] # AUDIT-C is not a trigger
results.append(_A(None, t0 + timedelta(hours=3), "phq9")) # a new PHQ-9 after the alert
assert await monitoring._drop_handled_crises(None, "t", [crisis]) == [crisis]