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.
This commit is contained in:
Marc-André Bombeck committed 2026-06-18 22:41:47 +02:00
1 parent 442eb83e2b
commit ddbb25d5d4
5 files changed
+98 -7

No files matched your search

@@ -0,0 +1,24 @@
-- 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);
+6
View File
@@ -3632,6 +3632,12 @@ model CoachMessage {
feedback RecommendationFeedback[]
@@index([conversationId, createdAt])
// v1.18.6 (L1) — the FAB nudge-status poll and the cron's persisted-nudge
// frequency gate both scan a user's assistant messages newest-first
// (optionally filtered to provider_type = 'nudge'). This index lets the
// planner do an index-backed per-conversation merge instead of gathering +
// sorting every assistant row in the user's threads on each global poll.
@@index([conversationId, role, createdAt(sort: Desc)])
@@map("coach_messages")
}