import { l as listAgentInstances, m as markCrashed } from './registry4-DHPSIQiW.js';
import './conduit-Ca_OFWR_.js';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';
import './unique-constraint-BM-Apx1H.js';
import './chunk-DDP5xfzh.js';
import './sqlite-Dsbe0wec.js';
import './dual-scope-db-ATrwq0LH.js';
import './memory-schema-Bi7cChwo.js';
import './agent-registry-BtE5uhMO.js';
import './nexus-sqlite2-DgTuv9BQ.js';
import './nexus-schema2-BEd-ol6e.js';
import './logger-ChCwlV5k.js';
import 'node:sqlite';
import './lock-BoHF25mx.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';

//#region ../core/dist/agents/health-monitor.js
/**
* Agent Health Monitoring -- Heartbeat and crash detection for live agent instances.
*
* Provides the public-facing health API specified by T039:
*   - `recordHeartbeat`    — update last_heartbeat for a live agent
*   - `checkAgentHealth`   — check health of a specific agent by ID
*   - `detectStaleAgents`  — find agents whose heartbeat is older than threshold
*   - `detectCrashedAgents` — find active agents with no heartbeat for >3 min
*
* These functions delegate to the lower-level `registry.ts` primitives
* (`heartbeat`, `checkAgentHealth`, `listAgentInstances`) and add the
* named, task-spec-aligned surface required for T039.
*
* @module agents/health-monitor
* @task T039
* @epic T038
*/
/** Default staleness threshold: 3 minutes without a heartbeat. */
var STALE_THRESHOLD_MS = 3 * 6e4;
/**
* Find agents with status `active` whose heartbeat has been silent for
* longer than `thresholdMs`, and mark them as `crashed` in the database.
*
* An agent is considered crashed when it:
* 1. Has status `active` (not `idle`, `starting`, `stopped`, or `crashed`)
* 2. Has not sent a heartbeat for longer than `thresholdMs`
*
* Each detected agent is immediately marked `crashed` via {@link markCrashed},
* incrementing its error count and writing a reason to `agent_error_log`.
*
* @param thresholdMs - Crash threshold in ms (default: 3 minutes / 180 000 ms)
* @param cwd - Working directory used to resolve the tasks.db path (optional)
* @returns Array of agent instance rows for each agent that was just marked
*   `crashed`, sorted by last heartbeat ascending (oldest first).
*
* @remarks
* This function is WRITE-side: it mutates the database. Callers should run
* it on a schedule (e.g. every 60 s) from an orchestrator or health watchdog.
* For a read-only view, use {@link detectStaleAgents} instead.
*
* @example
* ```ts
* // Inside an orchestrator health watchdog:
* const crashed = await detectCrashedAgents();
* if (crashed.length > 0) {
*   logger.warn({ crashed: crashed.map(a => a.id) }, 'Agents marked crashed');
* }
* ```
*/
async function detectCrashedAgents(thresholdMs = STALE_THRESHOLD_MS, cwd) {
	const activeAgents = await listAgentInstances({ status: "active" }, cwd);
	const cutoff = new Date(Date.now() - thresholdMs).toISOString();
	const crashed = [];
	for (const agent of activeAgents) if (agent.lastHeartbeat < cutoff) {
		const updated = await markCrashed(agent.id, `Heartbeat timeout — no heartbeat for >${Math.round(thresholdMs / 1e3)}s`, cwd);
		if (updated) crashed.push(updated);
	}
	crashed.sort((a, b) => {
		const aHb = a.lastHeartbeat ?? "";
		const bHb = b.lastHeartbeat ?? "";
		return aHb < bHb ? -1 : aHb > bHb ? 1 : 0;
	});
	return crashed;
}

export { STALE_THRESHOLD_MS, detectCrashedAgents };
//# sourceMappingURL=health-monitor-CaxN1A-_.js.map
