feat: crisis message after the test and non-blocking; weapon answer triggers nothing; ЕМИАС demo = Никита
CI / ruff · pytest (pull_request) Successful in 2m33s
CI / ruff · pytest (pull_request) Successful in 2m33s
- CRISIS_BLOCKING (default off for the demo): a crisis no longer locks the dialog; the message and the doctor's acute alert still happen. The clinical protocol (blocking, immediate stop) stays behind the flag and keeps its tests. - Crisis signals inside a questionnaire (intake red flags, high C-SSRS, PHQ-9 item 9) no longer stop it: the test is finished and the crisis message comes at the end; the doctor's alert is raised at once/at the end. - Weapon access: asked and stored, triggers nothing (rule disabled, no red flag, no safety block). - ЕМИАС demo persona is «Никита В.»; the one-click demo stays «Андрей С.». Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ Decisions:
|
||||
|
||||
from typing import Any
|
||||
|
||||
from app.config import get_settings
|
||||
from app.core.safety import CrisisResource, CrisisScript, crisis_reply
|
||||
from app.dialog.contracts import Reply
|
||||
from app.dialog.fsm import Ctx, Step
|
||||
@@ -37,6 +38,9 @@ def open_crisis(ctx: Ctx, categories: list[str], source: str) -> Step:
|
||||
text = "\n".join([ctx.texts.crisis.repeat, *_resource_lines(script.resources)])
|
||||
return Step(replies=[Reply(text=text, kind="crisis")])
|
||||
crisis = {"open": True, "opened_at": ctx.now.isoformat(), "source": source, "variant": script.variant}
|
||||
if not get_settings().crisis_blocking:
|
||||
# Non-blocking (demo default): the script is shown, the doctor is alerted, the patient keeps going.
|
||||
return Step(replies=[Reply(text=script.text, kind="crisis")], data={"crisis": crisis})
|
||||
return Step(
|
||||
replies=[Reply(text=script.text, kind="crisis")],
|
||||
next_state="crisis",
|
||||
|
||||
@@ -37,6 +37,7 @@ from weakref import WeakValueDictionary
|
||||
from sqlalchemy.exc import InterfaceError, OperationalError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.config import get_settings
|
||||
from app.core import clock, safety
|
||||
from app.core.intake_runtime import intake_enabled
|
||||
from app.core.privacy import scrub
|
||||
@@ -68,6 +69,9 @@ from app.services.contracts import MonitoringService, get_monitoring
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
LAST_PROMPT = "last_prompt"
|
||||
DEFERRED_CRISIS = "deferred_crisis"
|
||||
QUESTIONNAIRE_PREFIXES = ("scale.", "veteran.onboarding.", "intake.")
|
||||
DEFERRABLE_KINDS = frozenset({"crisis", "safety_plan"})
|
||||
MEDICINE_WORDS = re.compile(
|
||||
r"(лекарств|таблет|препарат|сертралин|пароксетин|флуоксетин|эсциталопрам|антидепрессант|побочк)", re.I
|
||||
)
|
||||
@@ -145,6 +149,8 @@ class Engine:
|
||||
|
||||
await self._apply(ctx, await self._start_pending(ctx), run)
|
||||
|
||||
self._defer_crisis_messages(ctx, event, run)
|
||||
|
||||
if _consented(ctx):
|
||||
for reply in run.replies:
|
||||
# Outgoing texts may quote the safety plan (names of relatives) — scrub before storing too.
|
||||
@@ -177,6 +183,21 @@ class Engine:
|
||||
)
|
||||
return run.replies
|
||||
|
||||
def _defer_crisis_messages(self, ctx: Ctx, event: Event, run: _Run) -> None:
|
||||
"""Crisis messages triggered by test answers wait until the questionnaire is over (team decision
|
||||
2026-10-04: let the patient finish the test, then show the crisis message; it blocks nothing). The doctor's
|
||||
alert is not delayed — only the patient-facing message. A typed acute message is never deferred."""
|
||||
if get_settings().crisis_blocking:
|
||||
return # clinical protocol: the crisis takes over immediately
|
||||
if event.type == "button" and ctx.state.startswith(QUESTIONNAIRE_PREFIXES):
|
||||
held = [r for r in run.replies if r.kind in DEFERRABLE_KINDS]
|
||||
if held:
|
||||
run.replies[:] = [r for r in run.replies if r.kind not in DEFERRABLE_KINDS]
|
||||
ctx.data[DEFERRED_CRISIS] = [*(ctx.data.get(DEFERRED_CRISIS) or []), *(r.model_dump() for r in held)]
|
||||
return
|
||||
if ctx.data.get(DEFERRED_CRISIS) and not ctx.state.startswith(QUESTIONNAIRE_PREFIXES):
|
||||
run.replies.extend(Reply.model_validate(r) for r in ctx.data.pop(DEFERRED_CRISIS))
|
||||
|
||||
def _abandoned(self, ctx: Ctx) -> bool:
|
||||
"""The current question step was left unanswered longer than `abandon_after_hours` (app.core.clock)."""
|
||||
if intake_enabled() or not _consented(ctx) or ctx.state in RESTING_STATES or crisis_open(ctx.data):
|
||||
|
||||
@@ -4,6 +4,7 @@ import math
|
||||
from dataclasses import asdict, replace
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from app.config import get_settings
|
||||
from app.core import interpretation
|
||||
from app.core.intake_questions import (
|
||||
DIARY_KINDS,
|
||||
@@ -15,6 +16,7 @@ from app.core.intake_questions import (
|
||||
group_answers,
|
||||
next_intake,
|
||||
red_flag_category,
|
||||
red_flag_triggers,
|
||||
scale_question,
|
||||
texts,
|
||||
)
|
||||
@@ -116,6 +118,39 @@ async def urgent(ctx, row=None, *, code="help_now", categories=None):
|
||||
return step
|
||||
|
||||
|
||||
def _defer_urgent(row, code, categories=None):
|
||||
"""Remember the first crisis signal of this questionnaire; the questionnaire goes on (team decision
|
||||
2026-10-04: the crisis message comes at the end and blocks nothing)."""
|
||||
if not row.details.get("pending_urgent"):
|
||||
row.details = {**row.details, "pending_urgent": {"code": code, "categories": categories or []}}
|
||||
|
||||
|
||||
async def _flush_urgent(ctx, row, step):
|
||||
"""Questionnaire over: acute alert + callback for the doctor, then the (non-blocking) crisis message."""
|
||||
pending = (row.details or {}).get("pending_urgent")
|
||||
if not pending or step.next_state == "intake.collect":
|
||||
return step
|
||||
row.details = {**row.details, "pending_urgent": None}
|
||||
code, categories = pending["code"], pending.get("categories") or []
|
||||
profile = await ctx.profile()
|
||||
if profile:
|
||||
profile.enhanced_monitoring = True
|
||||
await service.alert(
|
||||
ctx.session,
|
||||
ctx.token,
|
||||
f"{row.id}:{code}",
|
||||
code,
|
||||
ctx.now,
|
||||
level=AlertLevel.ACUTE,
|
||||
params={"record_id": row.id, "deferred": True},
|
||||
)
|
||||
await schedule_post_crisis_call(ctx.session, ctx.token, ctx.now, source="questionnaire:" + code)
|
||||
crisis = open_crisis(ctx, categories, source="questionnaire:" + code)
|
||||
for reply in crisis.replies:
|
||||
reply.buttons.append([Button(label=ctx.texts.safety_plan.title.rstrip(":"), data="safety_plan")])
|
||||
return step.then(crisis)
|
||||
|
||||
|
||||
async def item9_discrepancy(ctx, row):
|
||||
profile = await ctx.profile()
|
||||
if profile:
|
||||
@@ -170,18 +205,27 @@ async def manual_review(ctx, row, code):
|
||||
|
||||
|
||||
async def advance(ctx, row):
|
||||
return await _flush_urgent(ctx, row, await _advance(ctx, row))
|
||||
|
||||
|
||||
async def _advance(ctx, row):
|
||||
a = row.answers
|
||||
c = score_cssrs_recent(cssrs_answers(a))
|
||||
policy = load_policy()
|
||||
# Red flags (asked first): any «Да» stops the questionnaire with the help screen of its category.
|
||||
for item_id, value in a.items():
|
||||
if item_id.startswith("rf_") and value == 1:
|
||||
if item_id.startswith("rf_") and value == 1 and red_flag_triggers(item_id):
|
||||
category = red_flag_category(item_id)
|
||||
return await urgent(ctx, row, code="red_flag_" + item_id[3:], categories=[category] if category else [])
|
||||
if get_settings().crisis_blocking: # clinical protocol: stop at once
|
||||
return await urgent(ctx, row, code="red_flag_" + item_id[3:], categories=[category] if category else [])
|
||||
_defer_urgent(row, "red_flag_" + item_id[3:], [category] if category else [])
|
||||
break
|
||||
# One crisis rule for the whole app (ADR-013): the crisis screen opens at a HIGH C-SSRS risk only. Moderate →
|
||||
# stop + psychiatrist (about 24 h); low → flag, safety plan, continue.
|
||||
if c.risk_lower_bound == "high":
|
||||
return await urgent(ctx, row, code="suicide_signal")
|
||||
if get_settings().crisis_blocking:
|
||||
return await urgent(ctx, row, code="suicide_signal")
|
||||
_defer_urgent(row, "suicide_signal")
|
||||
if a.get("dphq_9", 0) > 0 and row.kind in DIARY_KINDS:
|
||||
# Diary depression module, item 9 ≥1 → the C-SSRS right away («Этап 2»); its risk level decides.
|
||||
return await item9_assessment(ctx, row)
|
||||
|
||||
@@ -33,6 +33,7 @@ from dataclasses import replace
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from app.config import get_settings
|
||||
from app.core.privacy import scrub
|
||||
from app.core.scales import Item, ScaleDef, ScaleResult, load_scale, score
|
||||
from app.db import repo
|
||||
@@ -237,7 +238,10 @@ async def _record(
|
||||
sex = profile.sex if profile is not None and profile.sex in ("m", "f") else None
|
||||
result = score(scale.id, answers, sex=sex) # type: ignore[arg-type]
|
||||
crisis_flags = [f for f in result.flags if f in ctx.texts.settings.crisis_flags]
|
||||
if crisis_flags or session["idx"] + 1 >= len(scale.items):
|
||||
# Clinical protocol (crisis_blocking): a crisis flag ends the scale at once. Demo default: the scale is finished
|
||||
# first and the crisis message comes at the end of the questionnaire (engine defers it), blocking nothing.
|
||||
stop_now = bool(crisis_flags) and get_settings().crisis_blocking
|
||||
if stop_now or session["idx"] + 1 >= len(scale.items):
|
||||
return await _finish(ctx, scale, session, answers, result, crisis_flags)
|
||||
updated = {**session, "answers": answers, "idx": session["idx"] + 1}
|
||||
sessions = {**(ctx.data.get("scales") or {}), scale.id: updated}
|
||||
@@ -278,7 +282,7 @@ async def _finish(
|
||||
# Crisis path decided by the scale's own flags (deterministic), not by the LLM.
|
||||
risk_screen = ctx.texts.settings.risk_screen_scale
|
||||
if crisis_open(ctx.data):
|
||||
crisis = Step(next_state="crisis")
|
||||
crisis = Step(next_state="crisis") if get_settings().crisis_blocking else Step()
|
||||
else:
|
||||
crisis = open_crisis(ctx, categories=[], source=f"scale:{scale.id}")
|
||||
step = done.then(crisis)
|
||||
|
||||
Reference in New Issue
Block a user