feat(relay2): typed close codes and Sentry exception capture
Summary
The observability items from the Orca comparison — the two that are just better, no behavioral hardening:
Typed close codes (RELAY_CLOSE, 4400–4410, in the shared tunnel-v2 protocol): every WS close now carries a fixed recovery meaning — 4401 auth-expired (mint a fresh token, reconnect), 4403 forbidden (config problem, retry slowly), 4404 unknown-ticket (abandon the stream), 4408 stale-host, 4409 replaced, 4410 tunnel-gone, 4400 bad-request — and the host client logs them by name via describeRelayClose. The Town-Hall incident's anonymous 1008 Forbidden retry loop would have read 4401 auth-expired: mint a fresh token and reconnect in the log.
Sentry (@sentry/cloudflare 10.68.0, matching the repo's Sentry train): withSentry on the Worker plus instrumentDurableObjectWithSentry on HostTunnel, so thrown exceptions from both surface with stacks. Console/breadcrumb capture is explicitly filtered out — the v1 relay's memory leak was Sentry's console firehose — and the integration is a no-op until the SENTRY_DSN secret is set (wrangler secret put SENTRY_DSN, needs a Sentry project). Adds the nodejs_als compat flag Sentry requires.
Workers Logs was already enabled in wrangler.jsonc — persistent, queryable logs exist in the CF dashboard today; nothing to add there.
Test plan
- typecheck clean (relay2, shared, host-service); lint clean
- Deployed and probed live: bad token →
4401 "Unauthorized", missing hostId →4400, bogus dial ticket →4404(this also exercises the Sentry-instrumented DO end-to-end) - Post-deploy health: both team hosts back online with fresh
lastSeenAtvia/presence - After a Sentry project + DSN secret exist: throw path visible in Sentry
Summary by cubic
Adds typed WebSocket close codes to the tunnel-v2 protocol and instruments relay2 with Sentry for exception capture, improving recovery guidance and error visibility. Previously the relay closed with generic 1008/1011; now it uses named RELAY_CLOSE codes (4400–4410) and the host logs them by name.
Review notes
- Protocol: adds
RELAY_CLOSEconstants anddescribeRelayCloseinpackages/shared; callers no longer infer behavior from reason strings. - Relay: replaces all closes with typed codes (
badRequest,authExpired,forbidden,unknownTicket,staleHost,replaced,tunnelGone) and updates the handshake helper to send a code + reason. - Host client:
packages/host-servicelogs close codes withdescribeRelayClose; no reconnect logic changes. - Sentry: adds
@sentry/cloudflareand wraps the Worker andHostTunnelwith Sentry; disables console/breadcrumb capture; no-op untilSENTRY_DSNis set; addsnodejs_alscompat flag andSENTRY_DSNtoRelayEnv.
Rollout
- Set the
SENTRY_DSNsecret in Cloudflare to enable capture:wrangler secret put SENTRY_DSN. - Deploy the
wrangler.jsoncchange (includescompatibility_flags: ["nodejs_als"]). - No client action required; hosts will log the new codes automatically.