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.

1.7 KB
Raw Blame
/**
 * How each frame an adapter sends for one viewer reaches it, logged every minute (`terminal relay`): delivered on this
 * process, or published, and how many subscriptions each publish reached (Redis answers that). A frame for one viewer
 * that wakes several processes is work for nothing (spec 2026-10-31-terminal-relay-direct). Counts only, never an id.
 */
import { logger } from '../utils/logger.js'

export type RelayKind = 'termup' | 'up targeted' | 'up broadcast' | 'pod hop' | 'pod json'
// `none` counts the publishes that reached no subscription: work for nobody, which is what the daemon's UI gate removes
// (spec 2026-11-02-swarm-cache-delta-design, Part 4).
interface Counts { local: number; published: number; receivers: number; none: number }

let kinds = new Map<RelayKind, Counts>()

export function recordRelay(kind: RelayKind, path: 'local' | 'published', receivers = 0): void {
  let counts = kinds.get(kind)
  if (!counts) kinds.set(kind, counts = { local: 0, published: 0, receivers: 0, none: 0 })
  if (path === 'local') counts.local++
  else { counts.published++; counts.receivers += receivers; if (receivers === 0) counts.none++ }
}

export function startRelayStats({ everyMs = 60_000, log = (message: string, context: Record<string, unknown>) => logger.info(message, context) }:
  { everyMs?: number; log?: (message: string, context: Record<string, unknown>) => void } = {}): () => void {
  let since = Date.now()
  const timer = setInterval(() => {
    const now = Date.now(), window = kinds
    kinds = new Map()
    if (window.size) log('terminal relay', { windowMs: now - since, kinds: Object.fromEntries(window) })
    since = now
  }, everyMs)
  timer.unref()
  return () => clearInterval(timer)
}