Repository navigation
feat(privacy): patient consent, data rights and retention - #6
Merged
Merged
Conversation
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.
9 tasks
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Patient consent and data rights before offering Artemisa to a health center (Chile, Ley 21.719).
consent_policies(published versions are frozen), an append-onlyconsent_eventslog (version +wamid),ACEPTOas the only word, honored only after that policy's text was shown; a new policy asks again.BAJAwithdraws consent,ELIMINARerases the sender's data (only the consent log is kept), andpurge_expired_personal_datadeletes conversations after 90 days without activity.Before deploying
007and008, then deploy thewebhookfunction.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).supabase/scripts/schedule_retention_purge.sqlby hand once that is confirmed.Test plan
deno test supabase/functions/_shared/phase1.test.ts)deno checkandnpx tsc --noEmitclean; Node tests: 13 passedsupabase/tests/consent_and_retention.test.sqlpass 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 cutoffmainshows only the requested reply changesKnown limits
ELIMINARcan recreate an empty conversation; the retention purge removes it.BAJA, the record that the policy was shown is kept, so a laterACEPTOcounts without showing the text again.🤖 Generated with Claude Code
https://claude.ai/code/session_01YSaSnVoTumjUbTz3yR7gjV