/**
 * Adaptive Heartbeat Service
 *
 * Implements self-driven, agent-side heartbeat scheduling that replaces
 * passive orchestrator-driven polling. Agents wake themselves at adaptive
 * intervals based on workload state:
 *
 * | State   | Interval | Behavior                              |
 * |---------|----------|---------------------------------------|
 * | Busy    | 5 min    | Full context scan, execute next task  |
 * | Idle    | 4 hours  | Check for new assignments             |
 * | Dormant | 24 hours | Alive check only                      |
 *
 * Inspired by Paperclip's heartbeat protocol.
 *
 * @see https://github.com/stevehuang0115/crewly/issues/172
 * @module services/agent/adaptive-heartbeat.service
 */
/** Heartbeat state determines scheduling interval */
export type HeartbeatState = 'busy' | 'idle' | 'dormant';
/** Adaptive heartbeat configuration */
export interface AdaptiveHeartbeatConfig {
    /** Interval in ms when agent has active tasks (default: 5 minutes) */
    busyIntervalMs: number;
    /** Interval in ms when agent has no tasks but team is active (default: 4 hours) */
    idleIntervalMs: number;
    /** Interval in ms when team is inactive (default: 24 hours) */
    dormantIntervalMs: number;
    /** Maximum consecutive idle heartbeats before transitioning to dormant */
    idleToDormantThreshold: number;
}
/** Result of a single heartbeat check */
export interface HeartbeatCheckResult {
    /** Current heartbeat state */
    state: HeartbeatState;
    /** Next heartbeat interval in ms */
    nextIntervalMs: number;
    /** Whether there is pending work */
    hasPendingWork: boolean;
    /** Number of pending tasks */
    pendingTaskCount: number;
    /** Number of pending messages */
    pendingMessageCount: number;
    /** ISO timestamp of this check */
    checkedAt: string;
}
/** Pending work summary returned by the backend API */
export interface PendingWorkSummary {
    /** Whether there is any pending work */
    hasPendingWork: boolean;
    /** Pending tasks assigned to this agent */
    pendingTasks: Array<{
        id: string;
        taskName: string;
        priority?: string;
        assignedAt: string;
    }>;
    /** Undelivered messages waiting for this agent */
    pendingMessages: Array<{
        id: string;
        from: string;
        preview: string;
        queuedAt: string;
    }>;
    /** Whether the agent's team is active */
    teamActive: boolean;
    /** Recommended heartbeat state based on current workload */
    recommendedState: HeartbeatState;
    /** Recommended next interval in ms */
    recommendedIntervalMs: number;
}
/** Default adaptive heartbeat configuration */
export declare const ADAPTIVE_HEARTBEAT_DEFAULTS: Readonly<AdaptiveHeartbeatConfig>;
/**
 * AdaptiveHeartbeatService manages self-driven agent heartbeats.
 *
 * Each agent instance runs this service to:
 * 1. Check for pending work via API
 * 2. Dynamically adjust heartbeat interval based on workload
 * 3. Report health status to the backend
 *
 * The service replaces passive orchestrator-driven polling (#169)
 * with agent-driven scheduling, reducing resource usage by ~80%
 * for idle agents.
 */
export declare class AdaptiveHeartbeatService {
    private readonly logger;
    private readonly config;
    private timer;
    private currentState;
    private consecutiveIdleChecks;
    private lastCheckResult;
    private onHeartbeat;
    private checkPendingWork;
    private sessionName;
    constructor(sessionName: string, config?: Partial<AdaptiveHeartbeatConfig>);
    /**
     * Set the callback for pending work checks.
     *
     * @param fn - Function that queries the backend for pending work
     */
    setCheckPendingWork(fn: (sessionName: string) => Promise<PendingWorkSummary>): void;
    /**
     * Set the callback invoked after each heartbeat.
     *
     * @param fn - Callback receiving the heartbeat check result
     */
    setOnHeartbeat(fn: (result: HeartbeatCheckResult) => void): void;
    /**
     * Start the adaptive heartbeat loop.
     * First heartbeat fires immediately, then reschedules adaptively.
     */
    start(): void;
    /**
     * Stop the adaptive heartbeat loop.
     */
    stop(): void;
    /**
     * Get the current heartbeat state.
     *
     * @returns Current state
     */
    getState(): HeartbeatState;
    /**
     * Get the last heartbeat check result.
     *
     * @returns Last result or null if no check has run
     */
    getLastResult(): HeartbeatCheckResult | null;
    /**
     * Get the interval for a given state.
     *
     * @param state - Heartbeat state
     * @returns Interval in ms
     */
    getIntervalForState(state: HeartbeatState): number;
    /**
     * Determine the next heartbeat state based on pending work.
     *
     * @param summary - Pending work summary from the backend
     * @returns Next heartbeat state
     */
    determineState(summary: PendingWorkSummary): HeartbeatState;
    /**
     * Execute a single heartbeat tick: check pending work, update state, schedule next.
     */
    private tick;
}
//# sourceMappingURL=adaptive-heartbeat.service.d.ts.map