The end-to-end push pipeline that replaces /api/control polling. This
commit ships the foundation; phases B-E light it up across the web,
desktop, and daemon. Read the architectural narrative in
/thoughts/from-polling-to-listening-fleetcrown-v0-6.
drizzle/0022_notify_triggers.sql
- Generic fc_emit_change() plpgsql function: receives (user_col,
key_col) via TG_ARGV, emits NOTIFY 'fc:state' with a compact
{t, u, k, op, ts} JSON payload. IDs only — no row content — so
the bus stays under Postgres's 8 KB NOTIFY limit even on bursts.
- AFTER INSERT/UPDATE/DELETE triggers on the five UI-relevant
tables: project_states, runtime_snapshots, pending_commands,
orchestration_runs, beacon_settings.
- Adding more tables in the future is two lines: drop + create
trigger pointing at the same fc_emit_change function.
bridge/ (new ~300-line Node service)
- src/server.ts — HTTP server with /sse (SSE endpoint, ?token=ck_*
auth, Last-Event-ID replay) and /healthz. CORS open; the bridge
sits on a different origin than Vercel and the desktop app.
- src/listen.ts — single dedicated pg Client running LISTEN
"fc:state" forever, with exponential-backoff reconnect on
disconnect. Notifications fan out to subscriptions.
- src/subscriptions.ts — in-memory Map<userId, Set<connection>>.
Per-user routing; fanout writes SSE frames; idempotent remove
on disconnect.
- src/auth.ts — validates ck_* tokens against agent_tokens using
the same plaintext lookup the cloud API uses (mirrors src/db/
queries/agent-tokens.ts validateAgentToken).
- src/types.ts — ChangeEvent and Subscription shapes.
- In-memory ring buffer (1000 events) for Last-Event-ID replay so
a brief disconnect doesn't drop state.
- Heartbeat (SSE comment) every 25s to keep proxies happy.
- Graceful SIGINT/SIGTERM shutdown with 5s hard-fail timeout.
- .env.example, .gitignore, README with the Oracle systemd deploy
runbook.
next.config.ts
- Exclude ./bridge/** from output: standalone tracing. Same lesson
as ./desktop/** — bridge has its own node_modules and is
deployed separately. Don't let Vercel bundle it into every
serverless function.
Verified end-to-end locally:
- psql UPDATE on project_states → fc_emit_change fires →
pg_notify reaches the bridge's LISTEN client → fanout to the
subscribed curl SSE connection in ~milliseconds.
- Per-user routing: a token bound to user A only receives events
for user A's rows; tested by minting under two different users.
- Last-Event-ID replay: reconnect with header skips already-
delivered events, then live events arrive immediately.
Next: Phase B (web client subscribes to the bridge instead of polling
/api/control). Phases C-E follow.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>