Files
HealthLog/prisma/migrations/0177_v1186_coach_message_role_index/migration.sql
T
Marc-André Bombeck ddbb25d5d4 fix(coach): cap proactive nudges by the persisted conversation, not push success
The proactive-nudge frequency gate only counted delivered push attempts. A
user with no push channel never wrote a delivered row, so the cap was inert
for them: every cron tick that hit a persistent trigger minted a fresh nudge
conversation in the rail.

Count a recently persisted nudge conversation toward the same rolling window
so the once-per-window contract holds regardless of push outcome. Add a
covering index on coach messages for the assistant-message scans the FAB poll
and the gate both run, and drop the unused providerType parameter from the
persistence helper now that the tag is fixed.
2026-06-18 22:41:47 +02:00

25 lines
1.3 KiB
SQL

-- v1.18.6 (L1) — covering index for the Coach assistant-message scans.
--
-- Two hot reads walk a user's assistant messages newest-first:
-- * the global Coach FAB poll (`GET /api/insights/coach/nudge-status`) —
-- the newest assistant message across the caller's conversations, used
-- to decide whether the unread dot paints; and
-- * the proactive-nudge cron's frequency gate (CCH-02 / M1) — the newest
-- `provider_type = 'nudge'` assistant message inside the rolling window.
--
-- The pre-existing `(conversation_id, created_at)` index does not cover the
-- `role` / `provider_type` filter, so Postgres gathered every assistant row
-- in the user's threads and sorted. `coach_messages` grows unbounded, and the
-- FAB poll fires on every authenticated page with refetch-on-focus.
--
-- This index orders by `created_at DESC` after `(conversation_id, role)` so
-- the planner can do an index-backed per-conversation merge for the
-- newest-first reads. Additive; no data touched.
--
-- Reversibility (down):
-- DROP INDEX IF EXISTS "coach_messages_conversation_id_role_created_at_idx";
-- CreateIndex
CREATE INDEX IF NOT EXISTS "coach_messages_conversation_id_role_created_at_idx"
ON "coach_messages"("conversation_id", "role", "created_at" DESC);