/** * OrchestratorEventBus — Central event stream for replay & time-travel debugging. * * Collects events from all orchestrator subsystems (blackboard, auth, adapters, * topology, decisions) into a single monotonically-sequenced stream with periodic * state snapshots for efficient point-in-time reconstruction. * * @module EventBus */ import { EventEmitter } from 'events'; /** Sources that can publish events */ export type EventSource = 'blackboard' | 'auth' | 'adapter' | 'topology' | 'decision' | 'budget' | 'quality' | 'injection' | 'orchestrator' | 'runtime' | 'custom'; /** Severity / importance of the event */ export type EventSeverity = 'trace' | 'info' | 'warn' | 'error'; /** A single event in the unified stream */ export interface BusEvent { /** Monotonic sequence number (global across all sources) */ seq: number; /** ISO 8601 timestamp */ timestamp: string; /** Which subsystem produced this event */ source: EventSource; /** Event type within the source (e.g. 'write', 'commit', 'permission_check') */ type: string; /** Severity level */ severity: EventSeverity; /** Agent involved, if any */ agentId?: string; /** Correlation id to group related events across subsystems */ correlationId?: string; /** Event-specific payload */ data: Record; } /** A point-in-time snapshot of orchestrator state for efficient replay */ export interface StateSnapshot { /** Sequence number at the time the snapshot was taken */ atSeq: number; /** ISO 8601 timestamp */ timestamp: string; /** Blackboard key→value dump */ blackboard: Record; /** Active agents and their status */ agents: Record; /** Budget state */ budget?: Record; /** Arbitrary metadata */ metadata?: Record; } /** Options for replaying events */ export interface ReplayOptions { /** Start sequence (inclusive). Defaults to 0 */ fromSeq?: number; /** End sequence (inclusive). Defaults to latest */ toSeq?: number; /** Filter by source(s) */ sources?: EventSource[]; /** Filter by agent */ agentId?: string; /** Filter by correlation id */ correlationId?: string; /** Filter by severity */ minSeverity?: EventSeverity; } /** Result of a replay query */ export interface ReplayResult { /** The nearest snapshot at or before fromSeq */ baseSnapshot: StateSnapshot | null; /** Events in the requested range */ events: BusEvent[]; /** Total events in the stream */ totalEvents: number; } /** * Central event bus for the orchestrator. * * Provides: * - Unified monotonic event stream from all subsystems * - Periodic state snapshots for O(1) point-in-time reconstruction * - Replay with filtering (source, agent, severity, correlation) * - Configurable retention (max events, max snapshots) * * @example * ```ts * const bus = new OrchestratorEventBus(); * bus.publish('blackboard', 'write', 'info', { key: 'x', value: 42 }, 'agent-1'); * bus.snapshot({ blackboard: bb.getSnapshot(), agents: {} }); * const replay = bus.replay({ fromSeq: 0, toSeq: 100 }); * ``` */ export declare class OrchestratorEventBus extends EventEmitter { private events; private snapshots; private seq; private maxEvents; private maxSnapshots; private snapshotInterval; private eventsSinceSnapshot; constructor(options?: { /** Max events to retain in memory. Default 50_000 */ maxEvents?: number; /** Max snapshots to retain. Default 100 */ maxSnapshots?: number; /** Take a snapshot every N events (0 = manual only). Default 0 */ snapshotInterval?: number; }); /** * Publish an event to the bus. * Returns the assigned sequence number. */ publish(source: EventSource, type: string, severity: EventSeverity, data: Record, agentId?: string, correlationId?: string): number; /** * Take a state snapshot at the current sequence position. */ snapshot(state: Omit): StateSnapshot; /** * Check if an automatic snapshot should be taken (called internally after publish). * Returns true if a snapshot is due. Callers must provide state via `snapshot()`. */ isSnapshotDue(): boolean; /** * Replay events with optional filtering. * Returns the nearest base snapshot + matching events. */ replay(options?: ReplayOptions): ReplayResult; /** * Get the event at a specific sequence number. */ getEvent(seq: number): BusEvent | undefined; /** * Get the most recent N events. */ recent(count: number): BusEvent[]; /** * Get all snapshots. */ getSnapshots(): StateSnapshot[]; /** * Find the nearest snapshot at or before a given sequence. */ snapshotAt(seq: number): StateSnapshot | null; /** Current sequence counter value (next event will get this seq). */ get currentSeq(): number; /** Total stored events. */ get size(): number; /** Total stored snapshots. */ get snapshotCount(): number; /** Clear all events and snapshots, reset sequence. */ clear(): void; /** * Export the full stream (events + snapshots) as a serializable object. */ export(): { events: BusEvent[]; snapshots: StateSnapshot[]; }; /** * Import a previously exported stream, merging into current state. */ import(data: { events: BusEvent[]; snapshots: StateSnapshot[]; }): void; } //# sourceMappingURL=event-bus.d.ts.map