/** * MailboxHealthWatchdog — probes the mailbox HTTP bridge and posts a * status message when it goes down. * * Designed to be embedded inside the WrongStack REPL/TUI/WebUI process — * NOT a standalone agent. WrongStack has its own cross-agent * coordination channel, so the cheapest reliable signal is "ask the * bridge if it's alive, and if it's not, tell the rest of the team * via the same channel the bridge exposes." * * Usage: * * const mailbox = new GlobalMailbox(projectDir); * const watchdog = new MailboxHealthWatchdog({ * mailbox, * url: 'http://127.0.0.1:7788', * probeIntervalMs: 15_000, * onAlert: (event) => console.warn(event), * }); * await watchdog.start(); * // ...later... * await watchdog.stop(); * * The watchdog is a passive observer: it does NOT start the bridge. * Starting the bridge is the user's job (`wstack mailbox serve` or * `/mailbox-serve`). The watchdog then reports on what the user did. */ import type { GlobalMailbox } from './global-mailbox.js'; import type { MailboxSendInput } from './mailbox-types.js'; export interface MailboxHealthWatchdogOptions { /** Project mailbox to probe-and-report on. Required. */ mailbox: GlobalMailbox; /** URL of the mailbox bridge (no trailing slash). Required. */ url: string; /** Probe interval in milliseconds. Default: 15_000. */ probeIntervalMs?: number; /** Per-probe timeout in milliseconds. Default: 3_000. */ probeTimeoutMs?: number; /** * After this many consecutive failures the watchdog posts an alert. * Default: 2 (so a single transient timeout doesn't trigger spam). */ failureThreshold?: number; /** * Optional callback for local observability — fired on every state * transition. The mailbox post is independent of this callback. */ onAlert?: ((event: MailboxHealthEvent) => void) | undefined; /** * Agent id used to post the alert message. Default: 'mailbox-bridge-watchdog'. */ from?: string; } export type MailboxHealthEvent = { kind: 'probe-failed'; status?: number; error?: string; } | { kind: 'alert-posted'; consecutiveFailures: number; } | { kind: 'recovery-posted'; downtimeMs: number; } | { kind: 'started'; intervalMs: number; } | { kind: 'stopped'; }; export declare const MAILBOX_HEALTH_DEFAULT_INTERVAL_MS = 15000; export declare const MAILBOX_HEALTH_DEFAULT_TIMEOUT_MS = 3000; export declare const MAILBOX_HEALTH_DEFAULT_FAILURE_THRESHOLD = 2; export declare const MAILBOX_HEALTH_DEFAULT_FROM = "mailbox-bridge-watchdog"; export declare class MailboxHealthWatchdog { private readonly mailbox; private readonly url; private readonly intervalMs; private readonly timeoutMs; private readonly failureThreshold; private readonly from; private readonly onAlert?; private timer; private consecutiveFailures; private downSince; private alerting; private inFlight; private aborted; constructor(opts: MailboxHealthWatchdogOptions); /** Start probing on `intervalMs`. Idempotent — second call is a no-op. */ start(): Promise; /** Stop probing. Safe to call multiple times. */ stop(): Promise; /** True between start() and stop(). */ isRunning(): boolean; /** Number of consecutive failed probes since the last successful probe. */ get currentFailureStreak(): number; /** True iff the watchdog currently considers the bridge down. */ isBridgeDown(): boolean; private emit; private tick; private probe; private recordSuccess; private recordFailure; private postDown; private postRecovery; } export interface DownAlertInput { from: string; url: string; consecutiveFailures: number; } export declare function buildDownAlert(input: DownAlertInput): MailboxSendInput; export interface RecoveryAlertInput { from: string; url: string; downtimeMs: number; consecutiveFailures: number; } export declare function buildRecoveryAlert(input: RecoveryAlertInput): MailboxSendInput; export interface WatchdogConfig { probeIntervalMs: number; probeTimeoutMs: number; failureThreshold: number; } /** * Throws if the watchdog config is invalid. Called from the * MailboxHealthWatchdog constructor so misconfiguration fails fast * (at startup), not silently after the first probe. */ export declare function validateWatchdogOptions(cfg: WatchdogConfig): void; //# sourceMappingURL=mailbox-health.d.ts.map