DocUA Claude Opus 5.5 (1M context) commited on
Commit
474586e
·
1 Parent(s): 948058d

fix: only explicit refusals decline the Spiritual Health referral

Browse files

Any consent answer that was not a clear yes or a question used to count as
a refusal, so a patient answering with more distress ("praying doesn't help
anymore") got "I understand and respect your decision" and lost the referral.
Keyword matching was also substring-based ("I know" hit "no", "мене" hit "не").

- Classify consent answers as accept / decline / unclear with word-boundary
patterns; negated accept words ("not ok") are not consent
- Short explicit refusals (<= 3 words) decline without an LLM call; longer
answers with refusal words are checked for distress first
- Unclear answer with new distress keeps the consent request open; otherwise
re-ask once, then close softly without a referral

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

src/core/simplified_medical_app.py CHANGED
@@ -14,6 +14,7 @@ Requirements: 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 2.4
14
  import os
15
  import sys
16
  import logging
 
17
  from datetime import datetime
18
  from dataclasses import asdict
19
  from typing import List, Tuple, Optional
@@ -199,7 +200,7 @@ class SimplifiedMedicalApp:
199
  # Route based on current spiritual state
200
  if self.spiritual_state.is_awaiting_consent():
201
  # Currently awaiting consent response - handle it
202
- response = self._handle_consent_response(message)
203
  elif self.spiritual_state.is_in_triage():
204
  # Currently in YELLOW state - continue triage
205
  response = self._handle_triage_response(message, conversation_context)
@@ -419,13 +420,16 @@ class SimplifiedMedicalApp:
419
 
420
  return consent_response
421
 
422
- def _handle_consent_response(self, message: str) -> str:
423
  """
424
  Handle patient's response to consent request.
425
 
426
  If patient asks about the service → explain it and keep awaiting consent
427
  If patient agrees → generate referral
428
- If patient declines → return to medical dialog without referral
 
 
 
429
 
430
  Requirement: Or_review.md section 4.A.3
431
  """
@@ -440,22 +444,77 @@ class SimplifiedMedicalApp:
440
  logger.info("Patient asked about the spiritual care team - explaining service")
441
  return self._explain_spiritual_care_service(patient_language)
442
 
443
- # Check if patient consents
444
- consent_given = self._check_consent(message)
445
 
446
- if consent_given:
447
  logger.info("Patient consented to spiritual care referral")
448
  return self._process_consent_accepted(patient_language)
449
- else:
450
- logger.info("Patient declined spiritual care referral")
451
- return self._process_consent_declined(patient_language)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452
 
453
  def _is_consent_question(self, message: str) -> bool:
454
  """
455
  Check whether the patient is asking about the spiritual care team rather
456
  than accepting or refusing the referral.
457
 
458
- This must be checked before _check_consent: phrases like "I don't know,
459
  who are they" contain negative keywords ("don't", "not") and would
460
  otherwise be scored as a refusal.
461
  """
@@ -497,56 +556,47 @@ They support people whatever their beliefs are, including people who are not rel
497
 
498
  Would you be open to having someone from the team reach out to you?"""
499
 
500
- def _check_consent(self, message: str) -> bool:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
  """
502
- Check if patient's response indicates consent.
503
 
504
- Returns True if patient agrees, False if declines or unclear.
 
 
505
  """
506
- message_lower = message.lower()
507
-
508
- # Positive consent indicators (English)
509
- positive_en = [
510
- "yes", "yeah", "yep", "sure", "okay", "ok", "please", "i would",
511
- "that would be", "i'd like", "sounds good", "go ahead", "i agree",
512
- "i consent", "you can", "that's fine", "i'm open", "i accept"
513
- ]
514
-
515
- # Positive consent indicators (Ukrainian)
516
- positive_uk = [
517
- "так", "добре", "гаразд", "будь ласка", "я згоден", "я згодна",
518
- "можна", "звичайно", "я хочу", "я б х��тів", "я б хотіла",
519
- "це було б", "я відкритий", "я відкрита", "погоджуюсь"
520
- ]
521
-
522
- # Negative consent indicators (English)
523
- negative_en = [
524
- "no", "nope", "not", "don't", "do not", "i don't want",
525
- "no thanks", "no thank you", "i'm not", "i am not",
526
- "i decline", "i refuse", "not interested", "not now",
527
- "maybe later", "not right now", "i'd rather not"
528
- ]
529
-
530
- # Negative consent indicators (Ukrainian)
531
- negative_uk = [
532
- "ні", "не", "не хочу", "не треба", "не потрібно",
533
- "дякую, ні", "я не хочу", "я відмовляюсь", "не зараз",
534
- "можливо пізніше", "краще ні", "не цікавить"
535
- ]
536
-
537
- # Check for negative first (more conservative)
538
- for neg in negative_en + negative_uk:
539
- if neg in message_lower:
540
- return False
541
-
542
- # Check for positive
543
- for pos in positive_en + positive_uk:
544
- if pos in message_lower:
545
- return True
546
-
547
- # If unclear, treat as decline (respect patient autonomy)
548
- return False
549
-
550
  def _process_consent_accepted(self, language: str) -> str:
551
  """Process accepted consent - generate and log referral."""
552
  # Get pending referral data
 
14
  import os
15
  import sys
16
  import logging
17
+ import re
18
  from datetime import datetime
19
  from dataclasses import asdict
20
  from typing import List, Tuple, Optional
 
200
  # Route based on current spiritual state
201
  if self.spiritual_state.is_awaiting_consent():
202
  # Currently awaiting consent response - handle it
203
+ response = self._handle_consent_response(message, conversation_context)
204
  elif self.spiritual_state.is_in_triage():
205
  # Currently in YELLOW state - continue triage
206
  response = self._handle_triage_response(message, conversation_context)
 
420
 
421
  return consent_response
422
 
423
+ def _handle_consent_response(self, message: str, conversation_context: Optional[List[str]] = None) -> str:
424
  """
425
  Handle patient's response to consent request.
426
 
427
  If patient asks about the service → explain it and keep awaiting consent
428
  If patient agrees → generate referral
429
+ If patient explicitly declines → return to medical dialog without referral
430
+ If the answer is unclear → never treat it as a refusal (Or_7 final test):
431
+ new distress → acknowledge and keep the consent request open
432
+ otherwise → gently re-ask once, then close softly without referral
433
 
434
  Requirement: Or_review.md section 4.A.3
435
  """
 
444
  logger.info("Patient asked about the spiritual care team - explaining service")
445
  return self._explain_spiritual_care_service(patient_language)
446
 
447
+ decision = self._classify_consent_response(message)
 
448
 
449
+ if decision == "accept":
450
  logger.info("Patient consented to spiritual care referral")
451
  return self._process_consent_accepted(patient_language)
452
+ if decision == "decline":
453
+ # Only a short, explicit refusal skips the distress check: longer
454
+ # answers like "no one cares anymore" or "не хочу жити" contain
455
+ # refusal words but are not refusals.
456
+ if len(message.split()) <= 3 or not self._shows_new_distress(message, conversation_context):
457
+ logger.info("Patient declined spiritual care referral")
458
+ return self._process_consent_declined(patient_language)
459
+ return self._keep_consent_open(patient_language)
460
+ return self._handle_unclear_consent(message, patient_language, conversation_context)
461
+
462
+ def _shows_new_distress(self, message: str, conversation_context: Optional[List[str]] = None) -> bool:
463
+ """Whether a consent-stage answer itself expresses distress (YELLOW or RED)."""
464
+ assessment = self.spiritual_monitor.classify(message, conversation_context)
465
+ return assessment.state in (SpiritualState.YELLOW, SpiritualState.RED)
466
+
467
+ def _keep_consent_open(self, language: str) -> str:
468
+ """Acknowledge new distress and repeat the consent question (state stays AWAITING_CONSENT)."""
469
+ logger.info("Consent answer with new distress - keeping consent request open")
470
+ if language == "Ukrainian":
471
+ return """Дякую, що поділились цим — схоже, вам зараз нелегко. Розмова з кимось із нашої команди духовного здоров'я може допомогти.
472
+
473
+ Чи були б ви відкриті до того, щоб хтось із команди зв'язався з вами?"""
474
+ return """Thank you for telling me that — it sounds like a lot to carry. Talking with someone from our Spiritual Health team might help.
475
+
476
+ Would you be open to having someone from the team reach out to you?"""
477
+
478
+ def _handle_unclear_consent(
479
+ self,
480
+ message: str,
481
+ language: str,
482
+ conversation_context: Optional[List[str]] = None
483
+ ) -> str:
484
+ """
485
+ Handle an answer that is neither consent, refusal, nor a question.
486
+
487
+ A patient who answers the consent question with more distress
488
+ ("praying doesn't help anymore") must not be read as declining.
489
+ """
490
+ if self._shows_new_distress(message, conversation_context):
491
+ return self._keep_consent_open(language)
492
+
493
+ pending = self.spiritual_state.pending_referral or {}
494
+ if not pending.get("reasked"):
495
+ logger.info("Unclear consent answer - re-asking once")
496
+ pending["reasked"] = True
497
+ self.spiritual_state.pending_referral = pending
498
+ if language == "Ukrainian":
499
+ return """Жодного тиску — я лише хочу переконатися, що правильно вас зрозумів. Чи хотіли б ви, щоб хтось із нашої команди духовного здоров'я зв'язався з вами? Достатньо відповісти «так» чи «ні»."""
500
+ return """No pressure at all — I just want to make sure I understood you. Would you like someone from our Spiritual Health team to reach out to you? A simple yes or no is fine."""
501
+
502
+ logger.info("Still no clear consent after re-asking - closing softly without referral")
503
+ self._process_consent_declined(language)
504
+ if language == "Ukrainian":
505
+ return """Нічого страшного — вирішувати зараз не обов'язково. Якщо згодом захочете, щоб хтось із нашої команди духовного здоров'я зв'язався з вами, просто скажіть мені.
506
+
507
+ Чи є щось інше, з чим я можу вам допомогти сьогодні?"""
508
+ return """That's okay — you don't need to decide now. If you'd like someone from our Spiritual Health team to reach out later, just let me know.
509
+
510
+ Is there anything else I can help you with today?"""
511
 
512
  def _is_consent_question(self, message: str) -> bool:
513
  """
514
  Check whether the patient is asking about the spiritual care team rather
515
  than accepting or refusing the referral.
516
 
517
+ This must be checked before _classify_consent_response: phrases like "I don't know,
518
  who are they" contain negative keywords ("don't", "not") and would
519
  otherwise be scored as a refusal.
520
  """
 
556
 
557
  Would you be open to having someone from the team reach out to you?"""
558
 
559
+ # Only explicit refusals count as a decline; anything unclear is re-asked
560
+ # rather than silently treated as "no" (Or_7 final test).
561
+ _DECLINE_PATTERNS = [
562
+ r"^\W*(?:no|nope|nah)\b", r"\bno,?\s+thanks?\b", r"\bno thank you\b", r"\bnot now\b", r"\bnot right now\b",
563
+ r"\bmaybe later\b", r"\bnot interested\b",
564
+ r"\bi (?:don'?t|do not) (?:want|need) (?:that|it|this|them|anyone|someone|any help|to be contacted)\b", r"\bi decline\b",
565
+ r"\bi refuse\b", r"\bi'?d rather not\b", r"\bnot for me\b",
566
+ r"\bi'?m (?:fine|okay|ok|good)\b(?! with)", r"\bi am (?:fine|okay|ok|good)\b(?! with)",
567
+ r"^\W*ні\b", r"\bне хочу\b", r"\bне треба\b", r"\bне потрібно\b", r"\bне зараз\b",
568
+ r"\bможливо пізніше\b", r"\bкраще ні\b", r"\bне цікавить\b", r"\bвідмовляюсь\b",
569
+ r"\bу мене все (?:добре|гаразд)\b", r"\bзі мною все (?:добре|гаразд)\b",
570
+ ]
571
+ _ACCEPT_PATTERNS = [
572
+ r"\byes\b", r"\byeah\b", r"\byep\b", r"\bsure\b", r"\bokay\b", r"\bok\b",
573
+ r"\bplease\b", r"\bi would\b", r"\bthat would be\b", r"\bi'?d like\b",
574
+ r"\bsounds good\b", r"\bgo ahead\b", r"\bi agree\b", r"\bi consent\b",
575
+ r"\bthat'?s fine\b", r"\bfine with\b", r"\bi'?m open\b", r"\bi accept\b",
576
+ r"\bтак\b", r"\bдобре\b", r"\bгаразд\b", r"\bбудь ласка\b", r"\bзгод(?:ен|на)\b",
577
+ r"\bможна\b", r"\bзвичайно\b", r"\bя хочу\b", r"\bя б хоті(?:в|ла)\b",
578
+ r"\bпогоджуюсь\b", r"\bвідкрит(?:ий|а)\b",
579
+ ]
580
+ _NEGATED_ACCEPT = r"\b(?:not|don'?t|doesn'?t|не)\s+(?:so\s+)?(?:sure|okay|ok|fine|good|добре|гаразд)\b"
581
+
582
+ def _classify_consent_response(self, message: str) -> str:
583
  """
584
+ Classify the patient's answer to the consent question.
585
 
586
+ Returns "accept", "decline" or "unclear". Explicit refusals win over
587
+ accept words ("no, I'm okay"); negated accept words ("not okay") are
588
+ not consent.
589
  """
590
+ message_lower = message.lower().replace("’", "'")
591
+
592
+ if any(re.search(p, message_lower) for p in self._DECLINE_PATTERNS):
593
+ return "decline"
594
+ if re.search(self._NEGATED_ACCEPT, message_lower):
595
+ return "unclear"
596
+ if any(re.search(p, message_lower) for p in self._ACCEPT_PATTERNS):
597
+ return "accept"
598
+ return "unclear"
599
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
600
  def _process_consent_accepted(self, language: str) -> str:
601
  """Process accepted consent - generate and log referral."""
602
  # Get pending referral data
tests/unit/test_or7_feedback.py CHANGED
@@ -78,3 +78,85 @@ def test_consent_manager_templates_use_spiritual_health_team():
78
  assert "confidential" not in joined
79
  assert "share your information" not in joined
80
  assert "Spiritual Health team" in " ".join(templates)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  assert "confidential" not in joined
79
  assert "share your information" not in joined
80
  assert "Spiritual Health team" in " ".join(templates)
81
+
82
+
83
+ # --- Consent answers: only explicit refusals decline (Or_7 final test) -------
84
+
85
+ import pytest
86
+
87
+ from src.core.spiritual_state import SpiritualAssessment, SpiritualState
88
+
89
+
90
+ def _awaiting_consent_app(monkeypatch, classified_as=SpiritualState.GREEN):
91
+ app = SimplifiedMedicalApp()
92
+ calls = []
93
+
94
+ def fake_classify(message, conversation_history=None):
95
+ calls.append(message)
96
+ return SpiritualAssessment(state=classified_as, indicators=[], confidence=0.9, reasoning="stub")
97
+
98
+ monkeypatch.setattr(app.spiritual_monitor, "classify", fake_classify)
99
+ red = SpiritualAssessment(state=SpiritualState.RED, indicators=["x"], confidence=0.9, reasoning="stub")
100
+ app.spiritual_state.last_assessment = red
101
+ app.spiritual_state.pending_referral = {"assessment": red, "language": "English", "original_message": ""}
102
+ app.spiritual_state.transition_to(SpiritualState.AWAITING_CONSENT, "test")
103
+ return app, calls
104
+
105
+
106
+ @pytest.mark.parametrize("message, expected", [
107
+ ("yes", "accept"), ("ok", "accept"), ("sure", "accept"), ("I'm fine with that", "accept"),
108
+ ("так", "accept"), ("добре", "accept"),
109
+ ("no", "decline"), ("No thanks", "decline"), ("not now", "decline"), ("I'm fine", "decline"),
110
+ ("no, I'm okay", "decline"), ("ні", "decline"), ("не зараз", "decline"),
111
+ ("a little bit both", "unclear"), ("praying doesn't help anymore", "unclear"),
112
+ ("I'm not ok", "unclear"), ("feeling worried", "unclear"),
113
+ ("I know", "unclear"), # used to hit the "no" substring and count as a refusal
114
+ ("мене це турбує", "unclear"), # used to hit the "не" substring
115
+ ])
116
+ def test_classify_consent_response(message, expected):
117
+ assert SimplifiedMedicalApp._classify_consent_response(SimplifiedMedicalApp, message) == expected
118
+
119
+
120
+ def test_new_distress_keeps_consent_open(monkeypatch):
121
+ app, _ = _awaiting_consent_app(monkeypatch, classified_as=SpiritualState.RED)
122
+
123
+ response = app._handle_consent_response("praying doesn't help anymore")
124
+
125
+ assert app.spiritual_state.is_awaiting_consent()
126
+ assert app.spiritual_state.pending_referral is not None
127
+ assert "respect your decision" not in response
128
+ assert "Would you be open to having someone from the team reach out to you?" in response
129
+
130
+
131
+ def test_long_answer_with_refusal_words_but_distress_is_not_a_decline(monkeypatch):
132
+ app, calls = _awaiting_consent_app(monkeypatch, classified_as=SpiritualState.RED)
133
+
134
+ app._handle_consent_response("No one cares about me anymore")
135
+
136
+ assert calls == ["No one cares about me anymore"]
137
+ assert app.spiritual_state.is_awaiting_consent()
138
+
139
+
140
+ def test_short_explicit_refusal_declines_without_llm(monkeypatch):
141
+ app, calls = _awaiting_consent_app(monkeypatch)
142
+
143
+ response = app._handle_consent_response("no thanks")
144
+
145
+ assert calls == []
146
+ assert app.spiritual_state.spiritual_state == SpiritualState.GREEN
147
+ assert app.spiritual_state.pending_referral is None
148
+ assert "respect your decision" in response
149
+
150
+
151
+ def test_unclear_answer_is_reasked_once_then_closed_softly(monkeypatch):
152
+ app, _ = _awaiting_consent_app(monkeypatch, classified_as=SpiritualState.GREEN)
153
+
154
+ first = app._handle_consent_response("a little bit both")
155
+ assert app.spiritual_state.is_awaiting_consent()
156
+ assert "A simple yes or no is fine." in first
157
+
158
+ second = app._handle_consent_response("hmm whatever")
159
+ assert app.spiritual_state.spiritual_state == SpiritualState.GREEN
160
+ assert app.spiritual_state.pending_referral is None
161
+ assert "you don't need to decide now" in second
162
+ assert "respect your decision" not in second