feat(chat/runs): alert-only zombie-owner check on scheduled runs

SW
4mo ago
+330-1

What

handleStartChatRun starts scheduled runs with no check that a human still uses the account — an abandoned account keeps generating (and billing) long after the owner left (recoupable/chat#1885, "Split out — billing & engagement integrity").

This fires a deduped Telegram alert when the owner's last role='user' message is older than 45 days (or they've never sent one). The run is NOT blocked — alert-only, per the issue spec.

Changes

  • getLatestUserMessageAt (lib/supabase/chat_messages/) — reads the owner's most recent role='user' message, walking chat_messages → chats → sessions via PostgREST inner embeds filtered on sessions.account_id.
  • isOwnerInactive — pure 45-day threshold predicate; null (never messaged) counts as inactive; exactly-at-threshold is still active.
  • markZombieOwnerAlerted — per-owner Redis SET NX EX dedup marker (30-day window) so daily scheduled runs don't spam; fails open on a Redis error so an infra blip never silences a real alert.
  • alertZombieOwner — orchestrates the read → detect → dedup → Telegram send; never throws.
  • handleStartChatRun — schedules the check via after() so it never blocks the 202 or the run.

Scope note

Detection uses chat_messages.role (the durable workflow chat surface the scheduled-run journey writes to) — the table with an explicit role column the issue references.

Verification

  • TDD RED→GREEN for every unit:
    • isOwnerInactive — null→inactive, >45d→inactive, <45d/at-threshold/today→active, threshold is 45.
    • markZombieOwnerAlerted — SET NX+EX returns true first time, false when marker exists, fails open on Redis error.
    • alertZombieOwner — alerts on >45d and on never-messaged, silent when active, deduped when marker claimed, never throws on dependency failure.
    • handleStartChatRun — fires the alert with the run's owner/chat/session after start; a throwing alert does not block the 202.
  • Full lib/chat/runs domain suite green — 35 tests passing.
  • tsc --noEmit: no errors in changed files.
  • eslint + prettier clean on all changed files.

Targets main.

Relates to recoupable/chat#1885 (split-out billing & engagement integrity).

🤖 Generated with Claude Code


Summary by cubic

Adds an alert-only zombie-owner check for scheduled chat runs. If the owner hasn’t sent a user message in 45+ days (or never), we send a deduped Telegram alert after the run starts without blocking it.

  • New Features
    • getLatestUserMessageAt: reads the owner’s latest role='user' message via Supabase embeds (chat_messages → chats → sessions filtered by sessions.account_id).
    • isOwnerInactive: pure 45-day predicate; null counts inactive; exactly 45 days is still active.
    • markZombieOwnerAlerted: per-owner Redis dedup (SET NX EX, 30-day TTL); fails open on Redis errors to avoid missing real alerts.
    • alertZombieOwner: orchestrates read → detect → dedup → Telegram send; never throws. Wired into handleStartChatRun via Next after() so it never delays the 202 or the run.