Files
loki/drizzle/0026_drop_legacy_project_states_trigger.sql
T
Mao Nakamoto 3079780b10 fix(db): drop duplicate project_states trigger + cap event-stream-token leak
Live Hetzner DB inspection on 2026-06-07 surfaced two real defects.

BUG 1 — DUPLICATE NOTIFY TRIGGERS ON project_states
The table had two trigger sets firing on every INSERT/UPDATE:
  * fc_notify_project_states (correct, from migration 0022 — emits on
    the canonical 'fc:state' channel via fc_emit_change)
  * project_states_notify (legacy pre-rename — emits on 'fleetcrown_state'
    via fleetcrown_notify_project_state, which nothing listens to)

Every state push fired both, doubling bridge NOTIFY traffic + giving
every SSE subscriber two events to process for one change.

drizzle/0026_drop_legacy_project_states_trigger.sql: drops the legacy
trigger + its function. Applied directly to prod via psql (verified
before: 5 trigger rows; after: 3). Idempotent (IF EXISTS).

BUG 2 — TOKEN TABLE LEAKS ON EVERY BROWSER LOAD
/api/event-stream-token mints a fresh ck_* token on every call. The
file's own comment (lines 33-39) literally said 'we always mint a fresh
one regardless'. Result: my account had 10 stale event-stream rows over
36 hours, all live, no cleanup. At any user scale agent_tokens grows
unboundedly.

Fix (three parts):

  a. NEW src/db/queries/agent-tokens.ts: getReusableEventStreamToken
     returns the most-recent event-stream token if last_used_at < 7 days,
     reused as-is.
  b. /api/event-stream-token: try reuse first, mint only as fallback.
     Response now includes {reused: true|false} for telemetry.
  c. NEW src/db/queries/agent-tokens.ts: deleteStaleEventStreamTokens
     deletes event-stream tokens with last_used_at older than 30 days
     (never touches other labels — daemon tokens are persistent).
  d. NEW src/app/api/crons/prune-agent-tokens/route.ts: Vercel Cron at
     05:00 UTC daily runs the cleanup, logs result to debug_logs as a
     heartbeat.
  e. vercel.json: register the new cron path.

ONE-TIME CLEANUP (already applied to prod during inspection):
  DELETE FROM agent_tokens WHERE label='event-stream' AND id NOT IN (
    SELECT id FROM (most-recently-used event-stream row per user)
  )
  Result: 10 stale rows → 1 active token per user.

Both fixes are deploy-on-Vercel-push safe; the SQL is already on prod.
Vercel deploy picks up the new cron route + endpoint change.

DEFERRED (separate batch): bootstrap drizzle.__drizzle_migrations
ledger so future migrations are traceable. The schema arrived via
'drizzle-kit push' so the migrations table doesn't exist; not blocking
this round.
2026-06-07 15:53:18 +02:00

25 lines
1.2 KiB
SQL

-- 0026 — drop legacy project_states_notify trigger + its function.
--
-- Pre-2026-06-07 inspection caught a duplicate trigger set on project_states:
-- * fc_notify_project_states (canonical — from 0022, fires on I/U/D, uses
-- the generic fc_emit_change function with TG_ARGV='user_id,project_key')
-- * project_states_notify (legacy — pre-rename, fires on I/U, calls a
-- standalone fleetcrown_notify_project_state function that emits a
-- different payload on the 'fleetcrown_state' channel)
--
-- Every UPDATE on project_states ran BOTH, doubling NOTIFY traffic on the
-- bridge and processing cost on every connected SSE subscriber. The canonical
-- fc_notify_project_states emits on 'fc:state' (the channel the bridge actually
-- listens on); the legacy one emits on 'fleetcrown_state' which nothing
-- subscribes to anymore — wasted work both sides.
--
-- This migration:
-- 1. DROP the legacy trigger
-- 2. DROP the legacy function it called
-- No data is touched. fc_notify_project_states keeps doing its job alone.
--
-- Safe to re-run (uses IF EXISTS).
DROP TRIGGER IF EXISTS project_states_notify ON project_states;
DROP FUNCTION IF EXISTS fleetcrown_notify_project_state();