Pre-v0.7: /api/beacon wrote one JSON file per session to
/tmp/<APP_SLUG>-beacon/<id>.json. The SSE route polled the directory
every 150ms with fs.readdirSync(). That worked while traffic was
single-pod-single-user, but had three real drawbacks:
- /tmp isn't durable; a function instance restart loses pending
popups silently
- Cross-pod reads impossible — a popup written by pod A is invisible
to pod B (latent landmine that hasn't bitten yet)
- Filesystem polling on every SSE tick is exactly the work
NOTIFY/LISTEN was added to obsolete in v0.6
This change moves storage to a beacon_sessions DB table. Same product
behavior, durable + queryable + user-scoped. A NOTIFY trigger on the
table is the v2 step that obsoletes the poll loop entirely — the
bridge already has the LISTEN side. This commit keeps the SSE poll
cadence to keep the diff small.
The beacon product feature itself (L3 of the autopilot ladder — the
"popup with countdown" tier between Manual and Continuous) is
unchanged. This was the file-based hack INSIDE the L3 product, not
the product itself.
Files:
+ drizzle/0024_beacon_sessions.sql (applied to Hetzner prod)
+ src/db/schema/beacon-sessions.ts
+ src/db/queries/beacon-sessions.ts
~ src/app/api/beacon/route.ts — POST + helpers delegate to DB
~ src/app/api/beacon/[id]/route.ts — GET/PATCH via DB
~ src/app/api/beacon/cancel/route.ts — userId-scoped (was global)
~ src/app/api/beacon/sse/route.ts — polls DB instead of /tmp,
requires auth (was unauthenticated)
~ src/app/api/inject/route.ts — passes userId to cancel
~ src/app/api/orchestration/run/route.ts — passes userId to cancel
~ src/app/beacon/[id]/page.tsx — await readBeaconSession (now async)
~ src/app/beacon/live/page.tsx — userId-scoped + await
+ src/db/schema/index.ts — export beacon-sessions
Behavior changes worth flagging:
- SSE endpoint now requires auth (returns 401 unauthenticated).
Pre-Change-4 it served any /tmp content. The file version's lack
of auth was incidental, not intentional.
- cancelActiveBeaconSessions / readLatestPendingSession became
user-scoped. Callers updated to thread userId through.
- beaconPath export removed — meaningless for DB-backed storage,
no remaining callers after the migration.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>