Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(backend): opt-in self-profile — CPU by kind each minute, 30 s profile every 15 min (#1814)

0xken15db683f18732c
diff --git a/backend/integration/bench/http.ts b/backend/integration/bench/http.ts
index 75265803d..ac68c03b1 100644
--- a/backend/integration/bench/http.ts
+++ b/backend/integration/bench/http.ts
@@ -1,5 +1,6 @@
import Fastify, { type FastifyInstance, type FastifyPluginAsync } from 'fastify'
import type { AuthUser } from '../../src/lib/ssoAuth.js'
+import { registerCpuKinds } from '../../src/lib/selfProfile.js'
import { errorHandler } from '../../src/middlewares/errorHandler.js'
import type { Reply } from './driver.js'
@@ -8,6 +9,7 @@ import type { Reply } from './driver.js'
export async function benchApp(users: () => readonly AuthUser[], routes: FastifyPluginAsync): Promise<FastifyInstance> {
const app = Fastify()
app.setErrorHandler(errorHandler)
+ registerCpuKinds(app)
app.addHook('onRequest', async (request, reply) => {
const user = users().find(u => `Bearer ${u.sub}` === request.headers.authorization)
if (!user) return reply.code(401).send({ success: false })
diff --git a/backend/integration/bench/routeCost.ts b/backend/integration/bench/routeCost.ts
new file mode 100644
index 000000000..7ba17455a
--- /dev/null
+++ b/backend/integration/bench/routeCost.ts
@@ -0,0 +1,90 @@
+import { performance } from 'node:perf_hooks'
+import { DEFAULT_CONFIG } from './config.js'
+import type { Reply } from './driver.js'
+import { partsDriver } from './drivers/parts.js'
+import { machineId, seedAccounts } from './fixtures.js'
+import { storeRoutes } from './legacyDriver.js'
+import { localDatabase } from './mongo.js'
+import { plan } from './plan.js'
+import { seedSwarm } from './seed.js'
+import { startSelfProfile } from '../../src/lib/selfProfile.js'
+
+/**
+ * What one call of each hot Swarm route costs the backend in CPU, alone, on the bench's seeded Swarm (#314: production
+ * stayed at five pods after the v1 release). `process.cpuUsage` counts every thread of this process, so the in-process
+ * query engine is included and MongoDB, which runs in its own process, is not. Multiply by production's per-route rate
+ * to rank where the CPU goes.
+ *
+ * NODE_ENV=test HARNESS_CREDENTIAL_ENCRYPTION_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= \
+ * DATABASE_URL=mongodb://127.0.0.1:1/bench-never-used npx tsx integration/bench/routeCost.ts [calls per route]
+ */
+async function main() {
+ const calls = Number(process.argv[2] ?? 200)
+ // CPU_SELF_PROFILE=1: the production self-profile on a short clock, to see its output under this load.
+ const stopProfile = process.env.CPU_SELF_PROFILE === '1'
+ ? startSelfProfile({ everyMs: 45_000, profileMs: 20_000, reportMs: 15_000, log: (m, c) => process.stdout.write(`${m} ${JSON.stringify(c, null, 1)}\n`) }) : () => {}
+ const config = { ...DEFAULT_CONFIG }
+ const database = await localDatabase()
+ const people = await seedAccounts(database.db, config.machines)
+ const driver = await partsDriver(database.db, () => people)
+ const { call, close } = await storeRoutes(database.db, () => people)
+ try {
+ const { swarm } = await seedSwarm(driver, config, plan(config), people)
+ const { tabId, workers, generations, leadId } = swarm, owner = people[0]!, w = workers[0]!, host = people[w.machine]!
+ // Production's polls are mostly idle: an agent with nothing waiting, readers whose cursor is past the last 30 s that
+ // every change read repeats (read/changes.ts). Take the worker's deliveries and wait out that window first.
+ for (const { id } of (await driver.deliveries(tabId, host, w.id)).data ?? []) {
+ await driver.delivery(tabId, host, w.id, id, { type: 'claim', claimId: `cost-${id}` })
+ await driver.delivery(tabId, host, w.id, id, { type: 'ack', claimId: `cost-${id}`, receipt: `cost-${id}`, uncertain: false })
+ }
+ const desktop = driver.desktopReader(tabId, owner), hostReader = driver.hostReader(host, machineId(w.machine))
+ await new Promise(resolve => setTimeout(resolve, 35_000))
+ await desktop.poll()
+ await hostReader.poll()
+ let n = 0, previousEventId: string | null = null
+ const routes: Record<string, () => Promise<Reply>> = {
+ 'GET deliveries (idle agent)': () => driver.deliveries(tabId, host, w.id),
+ 'POST hosts/:id/changes (host poll)': () => hostReader.poll(),
+ 'GET :tab/changes (Desktop poll)': () => desktop.poll(),
+ 'GET conversation messages': () => call(owner, 'GET', `/api/super-tabs/${tabId}/conversations/${w.group}/messages`),
+ 'GET lead messages': () => call(owner, 'GET', `/api/super-tabs/${tabId}/conversations/lead/messages`),
+ 'GET mentions': () => call(owner, 'GET', `/api/super-tabs/${tabId}/mentions?state=waiting`),
+ 'POST machines/:id (observation)': () => driver.observeMachine(tabId, owner, machineId(0), {
+ generation: generations[0]!, quotas: [], tools: {}, runningHarnessIds: ['bench-lead'], stoppedHarnessIds: [],
+ resources: { cpuPercent: 10 + (n++ % 50), memoryTotalBytes: 32e9, memoryUsedBytes: 4e9, diskFreeBytes: 120e9, diskTotalBytes: 500e9,
+ projectDiskFreeBytes: 120e9, deviceType: 'Laptop', chip: 'Bench', os: 'macOS', gpuPercent: null } }),
+ 'POST hosts/:id/terminal': () => call(host, 'POST', `/api/super-tabs/hosts/${machineId(w.machine)}/terminal`),
+ 'POST commands (message.send)': () => driver.execute(tabId, owner, `cost-${n++}`,
+ { type: 'message.send', conversationId: w.group, text: 'cost probe', replyTo: null, recipientIds: [] }, leadId),
+ 'POST native (terminal turn)': async () => {
+ const reply = await driver.appendNative(tabId, host, w.contextId,
+ { turnId: `cost-${n++}`, kind: 'prompt', previousEventId, text: 'cost probe', truncated: false, interrupted: false })
+ previousEventId = reply.data?.eventId ?? previousEventId
+ return reply
+ },
+ }
+ process.stdout.write(`| Route | Calls | Failed | CPU ms/call | Wall ms/call | Mean KB |\n|---|---|---|---|---|---|\n`)
+ for (const [label, run] of Object.entries(routes)) {
+ for (let i = 0; i < 5; i++) await run() // warm up
+ let failed = 0, bytes = 0
+ const cpu = process.cpuUsage(), wall = performance.now()
+ for (let i = 0; i < calls; i++) {
+ const reply = await run()
+ if (reply.code) failed++
+ bytes += reply.bytes
+ }
+ const used = process.cpuUsage(cpu), ms = (used.user + used.system) / 1000
+ process.stdout.write(`| ${label} | ${calls} | ${failed} | ${(ms / calls).toFixed(2)} | ${((performance.now() - wall) / calls).toFixed(2)} | ${(bytes / calls / 1024).toFixed(1)} |\n`)
+ }
+ } finally {
+ stopProfile()
+ await close()
+ await driver.close()
+ await database.stop()
+ }
+}
+
+main().catch(error => {
+ process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`)
+ process.exitCode = 1
+})
diff --git a/backend/src/lib/managerWs.ts b/backend/src/lib/managerWs.ts
index 2ca3e80e9..d58b45da6 100644
--- a/backend/src/lib/managerWs.ts
+++ b/backend/src/lib/managerWs.ts
@@ -10,6 +10,7 @@
* - on `up` → publishUp(machineId) so every client-holding backend fans it to its clients.
*/
import type { IncomingMessage } from 'http'
+import { frameKind, markCpuKind } from './selfProfile.js'
import type { Duplex } from 'stream'
import { randomUUID } from 'crypto'
import { WebSocket, type RawData } from 'ws'
@@ -43,6 +44,10 @@ const PRESENCE_TTL_SEC = 30
const wss = createWss(WS_LIMITS.manager)
+/** Frame types the CPU self-profile may log by name: the ones handled below; any other is logged as `other`. */
+const MANAGER_FRAME_TYPES: ReadonlySet<string> = new Set(['up', 'register', 'unregister', 'app_register', 'app_unregister', 'hello',
+ 'ping', 'provision_result', 'app_res', 'app_body', 'app_res_body', 'app_ws_msg', 'app_ws_close', 'app_abort'])
+
/** Fastify/http upgrade hook for `/api/manager-ws`. Auth = manager apiKey (`x-api-key`) + `managerId`. */
export function handleManagerUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void {
const apiKey = typeof req.headers['x-api-key'] === 'string' ? req.headers['x-api-key'] : undefined
@@ -226,6 +231,7 @@ export function attachManager(ws: WebSocket, managerId: string, poolKind: 'contr
const rawBytes = Buffer.isBuffer(raw) ? raw.length : Array.isArray(raw) ? raw.reduce((n, b) => n + b.length, 0) : raw.byteLength
let frame: ManagerFrame
try { frame = JSON.parse(raw.toString()) as ManagerFrame } catch { return }
+ markCpuKind(frameKind('manager-ws', frame.t, MANAGER_FRAME_TYPES))
switch (frame.t) {
case 'up': {
// Tap project lifecycle to maintain machine_agents (plan cap) — replaces the old /proxy
diff --git a/backend/src/lib/selfProfile.test.ts b/backend/src/lib/selfProfile.test.ts
new file mode 100644
index 000000000..f5c4d4ca4
--- /dev/null
+++ b/backend/src/lib/selfProfile.test.ts
@@ -0,0 +1,77 @@
+import Fastify from 'fastify'
+import { afterEach, expect, it } from 'vitest'
+import { frameKind, markCpuKind, selfProfileFromEnv, startSelfProfile, topSelfTime } from './selfProfile.js'
+
+let stop = () => {}
+afterEach(() => stop())
+
+it('ranks files and functions by self time, with dependencies by package and no absolute paths', () => {
+ const at = (id: number, functionName: string, url: string, lineNumber = 0) => ({ id, callFrame: { functionName, url, lineNumber } })
+ const top = topSelfTime({
+ nodes: [at(1, '(root)', ''), at(2, 'readChanges', `file://${process.cwd()}/dist/read/changes.js`, 9), at(3, 'deserialize', 'file:///app/node_modules/bson/lib/bson.cjs', 41)],
+ samples: [2, 3, 3, 3, 2], timeDeltas: [0, 1000, 2000, 3000, 4000],
+ })
+ expect(top).toEqual({ sampledMs: 10, files: { 'node_modules/bson/lib/bson.cjs': 9, 'dist/read/changes.js': 1 },
+ functions: { 'deserialize node_modules/bson/lib/bson.cjs:42': 9, 'readChanges dist/read/changes.js:10': 1 } })
+})
+
+it('counts the busy time of a named piece of work, and of what it awaits, under its name', async () => {
+ const logs: Record<string, unknown>[] = []
+ stop = startSelfProfile({ everyMs: 3_600_000, reportMs: 200, log: (_m, c) => logs.push(c) })
+ await new Promise<void>(resolve => setImmediate(() => {
+ markCpuKind('http GET /api/x')
+ void Promise.resolve().then(() => { const end = performance.now() + 40; while (performance.now() < end) { /* spin */ } }).then(() => resolve())
+ }))
+ await new Promise(resolve => setTimeout(resolve, 250))
+ const busy = logs[0]!.busyMs as Record<string, number>
+ expect(busy['http GET /api/x']).toBeGreaterThanOrEqual(35)
+})
+
+const spin = (ms: number) => { const end = performance.now() + ms; while (performance.now() < end) { /* spin */ } }
+const reported = async (logs: unknown[]) => { while (!logs.length) await new Promise(resolve => setTimeout(resolve, 20)) }
+
+it('logs a WebSocket frame by its type only when the type is known, anything a client invents as other', () => {
+ const known = new Set(['terminal_input'])
+ expect(frameKind('web-ws', 'terminal_input', known)).toBe('web-ws terminal_input')
+ expect(frameKind('web-ws', 'hello from [email protected]', known)).toBe('web-ws other')
+ expect(frameKind('manager-ws', { t: 'up' }, known)).toBe('manager-ws other')
+ expect(frameKind('manager-ws', undefined, known)).toBe('manager-ws other')
+})
+
+it('keeps at most 50 names in a window; work under any further name counts as other', async () => {
+ const logs: Record<string, number>[] = []
+ // One window long enough for all 120 names, so the cap is the only limit.
+ stop = startSelfProfile({ everyMs: 3_600_000, reportMs: 2_000, log: (_m, c) => logs.push(c.busyMs as Record<string, number>) })
+ for (let i = 0; i < 120; i++) {
+ await new Promise<void>(resolve => setImmediate(() => { markCpuKind(`job name${i}`); spin(1); resolve() }))
+ }
+ await reported(logs)
+ const busy = logs[0]!
+ expect(busy['job name0']).toBeGreaterThan(0)
+ expect(busy.other).toBeGreaterThanOrEqual(60)
+ expect(Object.keys(busy).filter(k => k.startsWith('job name')).length).toBeLessThanOrEqual(50)
+})
+
+it('names HTTP requests when CPU_SELF_PROFILE=1, and adds no request hook when it is off', async () => {
+ // Each server has its own route: work the first one started can still run in the second one's window.
+ const served = async (env: NodeJS.ProcessEnv, route: string) => {
+ const app = Fastify()
+ app.get(route, async () => { spin(5); return {} })
+ const logs: Record<string, number>[] = []
+ const options = { everyMs: 3_600_000, reportMs: 200, log: (_m: string, c: Record<string, unknown>) => logs.push(c.busyMs as Record<string, number>) }
+ const fromEnv = selfProfileFromEnv(app, env, options)
+ // Off, the profile is started by hand to see whether the request would still be named.
+ stop = fromEnv ?? startSelfProfile(options)
+ await app.inject({ method: 'GET', url: route })
+ await new Promise(resolve => setTimeout(resolve, 250))
+ stop(); stop = () => {}
+ await app.close()
+ return { started: fromEnv !== undefined, kinds: Object.keys(Object.assign({}, ...logs)) }
+ }
+ const on = await served({ CPU_SELF_PROFILE: '1' }, '/api/on')
+ expect(on.started).toBe(true)
+ expect(on.kinds).toContain('http GET /api/on')
+ const off = await served({}, '/api/off')
+ expect(off.started).toBe(false)
+ expect(off.kinds).not.toContain('http GET /api/off')
+})
diff --git a/backend/src/lib/selfProfile.ts b/backend/src/lib/selfProfile.ts
new file mode 100644
index 000000000..6cd646634
--- /dev/null
+++ b/backend/src/lib/selfProfile.ts
@@ -0,0 +1,136 @@
+import { createHook, executionAsyncResource } from 'node:async_hooks'
+import { Session } from 'node:inspector/promises'
+import { performance } from 'node:perf_hooks'
+import type { FastifyInstance } from 'fastify'
+import { logger } from '../utils/logger.js'
+
+/**
+ * Where this process spends its CPU, logged from production without a debugger (#314: five pods with few people).
+ * Off unless CPU_SELF_PROFILE=1. Two logs, neither with user data, only code locations and route/frame/job names:
+ *
+ * - `cpu by kind`, every minute: event-loop busy ms per kind of work, an HTTP route pattern, a web-ws or manager-ws
+ * frame type, or a timer's function name for background jobs. A kind is set where the work starts (markCpuKind) and
+ * follows every callback and promise that work creates, as AsyncLocalStorage does, so awaited DB replies count too.
+ * - `cpu profile`, 30 s every 15 min: the V8 sampling profiler of this process, top files and functions by self time.
+ *
+ * ponytail: the async hook adds overhead to every callback and promise (a few % in the bench); it is a diagnostic flag.
+ */
+const KIND = Symbol('cpuKind')
+type Tagged = { [KIND]?: string }
+let enabled = false
+let current: string | undefined, since = 0
+let busy = new Map<string, number>()
+/** Names one window keeps; work under any further name counts as `other`, so memory stays bounded. */
+const MAX_KINDS = 50
+
+/** Names the work running now, and everything it goes on to schedule. A no-op unless the profile is on. */
+export function markCpuKind(kind: string): void {
+ if (!enabled) return
+ const resource = executionAsyncResource() as Tagged
+ if (resource) resource[KIND] = kind
+ account(); current = kind
+}
+
+function account(): void {
+ const now = performance.now()
+ if (current !== undefined) {
+ const kind = busy.has(current) || busy.size < MAX_KINDS ? current : 'other'
+ busy.set(kind, (busy.get(kind) ?? 0) + now - since)
+ }
+ since = now
+}
+
+/** A WebSocket frame's kind: `<prefix> <type>` for a type in [known], else `<prefix> other`, so nothing a client
+ * sends becomes a log key. */
+export function frameKind(prefix: string, type: unknown, known: ReadonlySet<string>): string {
+ return `${prefix} ${typeof type === 'string' && known.has(type) ? type : 'other'}`
+}
+
+/** Every HTTP request is named by its route pattern, never its URL (no ids). */
+export function registerCpuKinds(app: FastifyInstance): void {
+ app.addHook('onRequest', async request => { markCpuKind(`http ${request.method} ${request.routeOptions.url ?? 'unrouted'}`) })
+}
+
+/** With CPU_SELF_PROFILE=1, starts the profile and names HTTP requests; off, adds nothing to the server. Returns its
+ * stop, or undefined when off. */
+export function selfProfileFromEnv(app: FastifyInstance, env: NodeJS.ProcessEnv = process.env, options?: SelfProfileOptions): (() => void) | undefined {
+ if (env.CPU_SELF_PROFILE !== '1') return undefined
+ registerCpuKinds(app)
+ return startSelfProfile(options)
+}
+
+export type SelfProfileOptions = { everyMs?: number; profileMs?: number; reportMs?: number; log?: (message: string, context: Record<string, unknown>) => void }
+
+export function startSelfProfile({ everyMs = 15 * 60_000, profileMs = 30_000, reportMs = 60_000,
+ log = (message, context) => logger.info(message, context) }: SelfProfileOptions = {}): () => void {
+ enabled = true
+ const hook = createHook({
+ init(_id, type, _trigger, resource) {
+ const inherited = (executionAsyncResource() as Tagged)?.[KIND]
+ const timer = type === 'Timeout' ? (resource as { _onTimeout?: { name?: string } })._onTimeout?.name : undefined
+ const kind = inherited ?? (type === 'Timeout' ? `job ${timer || 'anonymous'}` : undefined)
+ if (kind) (resource as Tagged)[KIND] = kind
+ },
+ before() { account(); current = (executionAsyncResource() as Tagged)?.[KIND] ?? 'untagged' },
+ after() { account(); current = undefined },
+ }).enable()
+
+ let cpu = process.cpuUsage(), wall = performance.now()
+ const report = setInterval(() => {
+ account()
+ const used = process.cpuUsage(cpu), elapsed = performance.now() - wall
+ const kinds = Object.fromEntries([...busy].sort((a, b) => b[1] - a[1]).slice(0, 40).map(([k, ms]) => [k, Math.round(ms)]))
+ log('cpu by kind', { windowMs: Math.round(elapsed), processCpuMs: Math.round((used.user + used.system) / 1000), busyMs: kinds })
+ busy = new Map(); cpu = process.cpuUsage(); wall = performance.now()
+ }, reportMs)
+ report.unref()
+
+ const profile = async () => {
+ const session = new Session()
+ session.connect()
+ try {
+ await session.post('Profiler.enable')
+ await session.post('Profiler.start')
+ await new Promise(resolve => setTimeout(resolve, profileMs).unref())
+ const { profile: p } = await session.post('Profiler.stop')
+ log('cpu profile', { profileMs, ...topSelfTime(p) })
+ } catch (error) {
+ logger.warn('cpu profile failed', { error: error instanceof Error ? error.message : String(error) })
+ } finally {
+ session.disconnect()
+ }
+ }
+ const first = setTimeout(() => void profile(), Math.min(everyMs, 60_000)).unref()
+ const every = setInterval(() => void profile(), everyMs).unref()
+ return () => { enabled = false; hook.disable(); clearInterval(report); clearTimeout(first); clearInterval(every) }
+}
+
+type Profile = { nodes: { id: number; callFrame: { functionName: string; url: string; lineNumber: number } }[]; samples?: number[]; timeDeltas?: number[] }
+
+export function topSelfTime(profile: Profile): { sampledMs: number; files: Record<string, number>; functions: Record<string, number> } {
+ const byId = new Map(profile.nodes.map(n => [n.id, n.callFrame]))
+ const files = new Map<string, number>(), functions = new Map<string, number>()
+ let total = 0
+ const samples = profile.samples ?? [], deltas = profile.timeDeltas ?? []
+ for (let i = 0; i < samples.length; i++) {
+ const frame = byId.get(samples[i]!), us = deltas[i + 1] ?? 0
+ if (!frame) continue
+ total += us
+ const file = shortFile(frame.url) || `(${frame.functionName || 'native'})`
+ files.set(file, (files.get(file) ?? 0) + us)
+ const fn = `${frame.functionName || '(anonymous)'} ${file}${frame.url ? `:${frame.lineNumber + 1}` : ''}`
+ functions.set(fn, (functions.get(fn) ?? 0) + us)
+ }
+ const top = (m: Map<string, number>, n: number) =>
+ Object.fromEntries([...m].sort((a, b) => b[1] - a[1]).slice(0, n).map(([k, us]) => [k, Math.round(us / 1000)]))
+ return { sampledMs: Math.round(total / 1000), files: top(files, 20), functions: top(functions, 30) }
+}
+
+/** `file:///app/dist/lib/hub.js` → `dist/lib/hub.js`; a dependency → `node_modules/<package>/<file>`. */
+function shortFile(url: string): string {
+ const path = url.replace(/^file:\/\//, '')
+ const modules = path.lastIndexOf('/node_modules/')
+ if (modules >= 0) return path.slice(modules + 1)
+ const cwd = `${process.cwd()}/`
+ return path.startsWith(cwd) ? path.slice(cwd.length) : path
+}
diff --git a/backend/src/lib/webWs.ts b/backend/src/lib/webWs.ts
index 8707ad313..9fe3a035c 100644
--- a/backend/src/lib/webWs.ts
+++ b/backend/src/lib/webWs.ts
@@ -17,6 +17,7 @@
* the external identity service.
*/
import type { IncomingMessage } from 'http'
+import { frameKind, markCpuKind } from './selfProfile.js'
import type { Duplex } from 'stream'
import { WebSocket, type RawData } from 'ws'
import { createWss, WS_LIMITS } from './wsServer.js'
@@ -68,6 +69,10 @@ export { BACKEND_ONLY_DOWN_TYPES } from './backendOnlyFrames.js'
const wss = createWss(WS_LIMITS.web, { echoFirstProtocol: true })
+/** Frame types the CPU self-profile may log by name; any other type a client sends is logged as `other`. */
+const WEB_FRAME_TYPES: ReadonlySet<string> = new Set([...TERMINAL_DOWN_TYPES, ...P2P_DOWN_TYPES,
+ 'p2p_result', 'e2e_hello', 'machine_select', 'machine_deselect', 'machines_watch', 'devices_watch', 'message'])
+
// Device-status re-seed: covers the silent-offline case (worker crash → presence TTL 45s ages out
// with no transition published) — the watcher re-reads presence keys on this cadence.
const DEVICE_RESEED_MS = 30_000
@@ -584,6 +589,7 @@ function attachUserClient(ws: WebSocket, user: AuthUser, surface: 'mobile' | 'we
}
let frame: Frame
try { frame = JSON.parse(raw.toString()) as Frame } catch { return }
+ markCpuKind(frameKind('web-ws', frame.type, WEB_FRAME_TYPES))
inbox.enqueue(frame)
})
@@ -643,6 +649,7 @@ function attachWebClient(ws: WebSocket, machineId: string): void {
let frame: Frame
try { frame = JSON.parse(raw.toString()) as Frame } catch { return }
const type = frame.type as string | undefined
+ markCpuKind(frameKind('web-ws', type, WEB_FRAME_TYPES))
// The same backend-only refusal as the per-user path: a key holder is a client, not the backend.
if (isBackendOnlyDownType(type)) return
if (typeof type === 'string' && type.startsWith('terminal_') && !TERMINAL_DOWN_TYPES.has(type)) return
diff --git a/backend/src/server.ts b/backend/src/server.ts
index 4301dec4b..bb3765b31 100644
--- a/backend/src/server.ts
+++ b/backend/src/server.ts
@@ -20,6 +20,7 @@ import { logger } from './utils/logger.js'
import { startTurnCredentialRefresh } from './lib/turnCredentials.js'
import { closeBus, redisCount, redisSsoProfileStore } from './lib/bus.js'
import { useSharedSsoProfileStore } from './lib/ssoAuth.js'
+import { selfProfileFromEnv } from './lib/selfProfile.js'
import { drainAllSockets, openSocketCount, RELEASE_UPGRADE_SLOT, type SlotSocket } from './lib/wsServer.js'
// Node ≥ 15 turns an unhandled rejection into a process exit. On a cluster worker that holds thousands of
@@ -118,6 +119,8 @@ const app = Fastify({
async function start(): Promise<void> {
// Mint the Cloudflare TURN credential before the first socket lands; no-op when TURN is unconfigured.
startTurnCredentialRefresh()
+ // Off by default: logs this process's CPU by kind of work every minute, and a 30 s profile every 15 min (#314).
+ selfProfileFromEnv(app)
app.setErrorHandler(errorHandler)