Skip to content

feat(privacy): patient consent, data rights and retention - #6

Merged
juancodedev merged 13 commits into
mainfrom
feat/patient-consent-and-data-rights
Nov 10, 2026
Merged

juancodedev merged 13 commits into
mainfrom
feat/patient-consent-and-data-rights

Conversation

@juancodedev

Copy link
Copy Markdown
Owner

Summary

Patient consent and data rights before offering Artemisa to a health center (Chile, Ley 21.719).

  • Crisis: detection covers real first-person phrasing, the helpline (*4141) is always sent first even when the database fails, and nothing is stored for a sender without consent.
  • No false promises: the bot no longer says a professional will follow up; nobody reads this channel. All patient-facing copy is neutral Spanish.
  • Versioned consent: consent_policies (published versions are frozen), an append-only consent_events log (version + wamid), ACEPTO as the only word, honored only after that policy's text was shown; a new policy asks again.
  • Rights and retention: BAJA withdraws consent, ELIMINAR erases the sender's data (only the consent log is kept), and purge_expired_personal_data deletes conversations after 90 days without activity.
  • Minimization: clinical and crisis text is stored as a marker and never sent to Claude.

Before deploying

  1. Apply migrations 007 and 008, then deploy the webhook function.
  2. The policy is seeded as an unpublished draft (2026-11-borrador). Until it is published, nobody can consent and the bot answers only fixed questions (price, hours, address, booking). Publish it only after legal review and once a public policy URL exists (docs/09-privacidad-y-consentimiento.md).
  3. Consents from migration 006 are not carried over: those senders are asked again.
  4. The 90-day purge is not scheduled. Its first run deletes every conversation already older than 90 days and cannot be undone. Run supabase/scripts/schedule_retention_purge.sql by hand once that is confirmed.

Test plan

  • Edge tests: 55 passed (deno test supabase/functions/_shared/phase1.test.ts)
  • deno check and npx tsc --noEmit clean; Node tests: 13 passed
  • SQL tests supabase/tests/consent_and_retention.test.sql pass on Postgres 17 (migrations 001-008); they fail if the append-only trigger is disabled, if ELIMINAR skips a table, or if the purge ignores its cutoff
  • Independent verification: no blockers; a differential check against main shows only the requested reply changes
  • Not observed on a real Supabase project (grants were checked on plain Postgres with stub roles)

Known limits

  • Crisis and clinical detection is a keyword list: some clinical wording ("estoy deprimida", "tomo sertralina") still reaches Claude for consented senders.
  • A message that already passed the gate a few milliseconds before ELIMINAR can recreate an empty conversation; the retention purge removes it.
  • After BAJA, the record that the policy was shown is kept, so a later ACEPTO counts without showing the text again.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YSaSnVoTumjUbTz3yR7gjV

Crisis signals are now matched with patterns that cover first-person phrasing such
as 'me quiero matar' and 'me voy a suicidar', which the keyword list missed.

The webhook answers a crisis before the profile lookup, the rate limit, and consent,
so no database outage can withhold the helpline. The redacted exchange is stored
only for a sender who consented to history.
Nobody monitors the channel, so the clinical referral and Claude's fallbacks no
longer promise that the professional will get in touch; they point the patient to
a channel that reaches a person. The router and Claude's prompt share one clinical
response.

Patient-facing replies move from voseo to neutral Spanish for the first market,
Chile.
A crisis match replaces the patient's request with the helpline, so 'me mato' and
'terminar con todo' now match only when they end the thought. 'Me mato estudiando'
and 'terminar con todo el papeleo' reach the ordinary pipeline again.

A failed helpline send is pinned to ask Meta for redelivery.
Patient text classified as clinical, or with clinical wording inside another route,
is stored as a fixed marker, as crisis text already was. History is redacted again
right before it is sent to Claude, which also covers rows stored before this change.

Detection is the existing keyword heuristic, so a clinical message it misses still
passes through.
Requiring 'me mato' and 'terminar con todo' to end the sentence missed 'esta noche
me mato' and 'quiero terminar con todo esto'. Both patterns now match by default and
exclude only known idiomatic continuations such as 'me mato estudiando' and
'terminar con todo el papeleo'.
Consent is now informed and provable. Each policy version is stored and frozen once
published, the sender must have been shown the text of that version before ACEPTO
counts, and every grant is a new row in an append-only log that records the version
and the WhatsApp message.

ACEPTO is the only opt-in word. A newer policy asks every sender again, and the
consents recorded under migration 006 are not carried over.

The first policy is seeded as an unpublished draft: until it is published, nobody
can consent and the bot answers only fixed listing questions.
Senders can now withdraw consent with BAJA and erase their data with ELIMINAR over
WhatsApp. Both run before the rate limit and without needing consent, and the bot says
so honestly when the database fails. ELIMINAR deletes the conversation, prompts,
counters and legacy consent, and keeps only the consent log as proof.

purge_expired_personal_data deletes conversations without activity for 90 days and
other personal data past its use. It is not scheduled by the migration: the first run
cannot be undone, so supabase/scripts/schedule_retention_purge.sql is a manual step.

Also from the consent review: a failed consent read is no longer reported as "no
policy", the service role can no longer write consent policies, and crisis phrasing
such as "me mato con las pastillas" is now detected.
The exchange is no longer stored when BAJA or ELIMINAR arrived while the message was
being answered: consent is read again right before storing. ELIMINAR also keeps the
last two hours of rate counters, so it cannot be used to reset the rate limit.
@juancodedev
juancodedev merged commit f14e6a6 into main Nov 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant