mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:47:22 +01:00
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>
32 lines
1.4 KiB
SQL
32 lines
1.4 KiB
SQL
-- Beacon popup sessions — Change 4.
|
|
--
|
|
-- Replaces the /tmp/<APP_SLUG>-beacon/<id>.json file-based store the L3
|
|
-- autopilot tier used. Same data shape, but durable across function
|
|
-- restarts, queryable cross-pod, and amenable to the LISTEN/NOTIFY event
|
|
-- bus (a follow-up trigger on this table will obsolete the 150ms SSE
|
|
-- polling loop in /api/beacon/sse).
|
|
--
|
|
-- Mirrors src/db/schema/beacon-sessions.ts. The pending-popup composite
|
|
-- index covers the /beacon/[id]/page.tsx SSR-preload query
|
|
-- (user_id + choice IS NULL + expires_at > now()).
|
|
|
|
CREATE TABLE IF NOT EXISTS beacon_sessions (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
project TEXT NOT NULL,
|
|
session_content TEXT NOT NULL DEFAULT '',
|
|
current_agent TEXT,
|
|
next_agent TEXT,
|
|
capacity_issue BOOLEAN NOT NULL DEFAULT false,
|
|
countdown_seconds INTEGER NOT NULL,
|
|
popup_mode TEXT NOT NULL,
|
|
git_branch TEXT,
|
|
choice TEXT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
expires_at TIMESTAMPTZ NOT NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_beacon_sessions_user_id ON beacon_sessions (user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_beacon_sessions_project ON beacon_sessions (project);
|
|
CREATE INDEX IF NOT EXISTS idx_beacon_sessions_pending ON beacon_sessions (user_id, choice, expires_at);
|