Files
loki/bridge
65dea6f303 chore: pnpm — the fleet has one package manager (#478)
* chore: pnpm — the fleet has one package manager

- packageManager pnpm@11.25.0; lockfile via pnpm import, package-lock.json
  removed; npm "workspaces" replaced by pnpm-workspace.yaml packages
  (packages/agent), and bridge/ joins the workspace — one install, one
  lockfile, its own package-lock.json removed
- npm "overrides" (fast-uri pin) moved to pnpm-workspace.yaml (pnpm ignores
  the npm-only field)
- allowBuilds: better-sqlite3, node-pty, @sentry/cli, native toolchain, and
  the two git-hosted deps (@fleet/ai-forms, limitkit — pnpm wants the exact
  resolved spec, keys move with the pinned tags); temporary
  minimumReleaseAgeExclude for our own @bitbaum/ai-kit@0.6.2
- phantom deps declared: @auth/core (src/auth.ts augments @auth/core/jwt —
  TS2664 under pnpm's strict layout), dotenv, sharp (scripts)
- CI/audit/deploy workflows: pnpm/action-setup@v6, store-dir pinned outside
  the action's bin dir, cache: pnpm, frozen-lockfile; deploy's separate
  `npm --prefix bridge ci` is covered by the workspace install; Next cache
  key hashes pnpm-lock.yaml; npm audit → pnpm audit
- desktop-release: root install → pnpm; desktop/ itself STAYS an npm package
  (own package-lock.json, electron toolchain untouched)
- husky: pre-push staleness gate now compares pnpm-lock.yaml against
  node_modules/.modules.yaml; hooks + scripts (deploy-hetzner, hetzner/deploy,
  install-app, doctor, smoke) converted to pnpm
- check-deploy-ready fleet gate now recognizes pnpm verify/test lines too

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

* fix: prettier-format pnpm-workspace.yaml — format:check gates verify

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

---------

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-04 10:35:27 +00:00
..
2026-08-31 09:23:30 +00:00

FleetCrown event bridge

A ~300-line Node service that takes Postgres NOTIFY events on the fc:state channel and fans them out to subscribed browsers, desktop apps, and phones over Server-Sent Events. The piece that lets v0.6 replace polling with push.

What runs where

                                         ┌───────────────────┐
                                         │  this service     │
   Browser ─────── /sse?token=ck_* ──────┤  (bridge/)        │
                                         │                   │
   Fleet Runner ── /sse?token=ck_* ──────┤   one HTTP+SSE    │
                                         │   server, one     │
                                         │   pg LISTEN       │
                                         │                   │
                                         └────────┬──────────┘
                                                  │ LISTEN "fc:state"
                                                  ↓
                                         ┌───────────────────┐
                                         │   Postgres        │
                                         │   triggers in     │
                                         │   drizzle/0022    │
                                         └───────────────────┘
                                                  ↑
                                                  │ INSERT/UPDATE/DELETE
                                         ┌────────┴──────────┐
                                         │   Hosted API      │
                                         │   /api/control,   │
                                         │   /api/control/   │
                                         │   runtime-state,  │
                                         │   etc.            │
                                         └───────────────────┘

Read the architectural narrative in /thoughts/from-polling-to-listening-fleetcrown-v0-6 for the why.

Run locally

cd bridge
cp .env.example .env
# edit .env — point DATABASE_URL at the same db that has the v0.6 triggers
npm install
npm run dev      # tsx — fast iteration
# or
npm run build && npm start

Health: curl http://localhost:4001/healthz → {ok:true, users, connections, eventLogSize}

SSE: curl -N "http://localhost:4001/sse?token=ck_..." (replace with a real ck_* token from your agent_tokens table). Watch as event: change frames arrive whenever a row owned by that user changes.

Auth model

Tokens are looked up in the same agent_tokens table the cloud API uses. Plaintext lookup (matches the existing scheme; see src/db/queries/agent-tokens.ts). The token is passed as ?token=ck_... in the SSE URL because EventSource does not let JavaScript set custom request headers. Mitigated by HTTPS-only deploy + scrubbing query strings from access logs.

Last-Event-ID replay

Clients that disconnect briefly (sleep, wifi drop, deploy) reconnect with Last-Event-ID: <last_id> in their headers. The bridge replays any buffered events with a higher id, filtered to the user's scope. Buffer size is 1000 events — anything older requires a full re-fetch from the cloud snapshot endpoint.

Deploy (Oracle / Hetzner)

The bridge lives on the same box as Postgres. Both processes plus a small backup cron fit comfortably on Oracle Always Free or a Hetzner CX22. See scripts/db/SETUP_ORACLE_FREE.md and scripts/db/BOTH_PRODUCTS_ONE_HOST.md for the host setup. Once Postgres is up:

# On the box
git clone git@github.com:bitbaum/fleetcrown.git /opt/fleetcrown
cd /opt/fleetcrown/bridge
npm ci
npm run build

# /etc/systemd/system/fleetcrown-bridge.service
sudo tee /etc/systemd/system/fleetcrown-bridge.service <<'UNIT'
[Unit]
Description=FleetCrown event bridge (Postgres LISTEN → SSE)
After=postgresql.service network-online.target
Wants=network-online.target

[Service]
Type=simple
User=fleetcrown
WorkingDirectory=/opt/fleetcrown/bridge
EnvironmentFile=/opt/fleetcrown/bridge/.env
ExecStart=/usr/bin/node /opt/fleetcrown/bridge/dist/server.js
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
UNIT

sudo systemctl daemon-reload
sudo systemctl enable --now fleetcrown-bridge
sudo journalctl -u fleetcrown-bridge -f

Then reverse-proxy 443 → 4001 via Caddy with a TLS cert (this is how the bitbaum box runs it today, as fleetcrown-bridge.service behind bridge.orangecat.ch). Point the web app's FLEETCROWN_BRIDGE_URL env at https://your-host/sse and the web client will subscribe instead of polling.

Connection limits

Default Linux file descriptor limit (1024) caps connections per process. Bump in the systemd unit before going to scale:

[Service]
LimitNOFILE=65536

One process easily handles ~50,000 concurrent SSE connections on the Oracle Free shape (4 OCPU / 24 GB). Past that, run multiple bridge processes behind a load balancer; they share state via the Postgres LISTEN channel automatically (NOTIFY fans out to every LISTEN'er).