import { c as checkAgentHealth, u as updateAgentStatus, l as listAgentInstances } 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';

/**
* Attempt to recover crashed agents.
*
* Finds all agents with status 'crashed' and determines if they can be
* restarted based on their error history. Agents whose last error was
* classified as 'permanent' are abandoned. Agents with retriable errors
* are reset to 'starting' for the orchestration layer to re-assign.
*
* @remarks
* Two-phase process: first detects stale agents via heartbeat threshold,
* then evaluates each crashed agent's error history for recoverability.
*
* @param thresholdMs - Heartbeat threshold for crash detection (default: 30000)
* @param cwd - Working directory
* @returns Recovery results for each crashed agent
*
* @example
* ```ts
* const results = await recoverCrashedAgents(60_000);
* results.filter(r => r.recovered).forEach(r => console.log(r.agentId));
* ```
*/
async function recoverCrashedAgents(thresholdMs = 3e4, cwd) {
	const results = [];
	const staleAgents = await checkAgentHealth(thresholdMs, cwd);
	for (const agent of staleAgents) await updateAgentStatus(agent.id, {
		status: "crashed",
		error: "Heartbeat timeout detected during recovery sweep"
	}, cwd);
	const crashedAgents = await listAgentInstances({ status: "crashed" }, cwd);
	for (const agent of crashedAgents) {
		const { getAgentErrorHistory } = await import('./registry3-BKYEGGK8.js');
		const errors = await getAgentErrorHistory(agent.id, cwd);
		const lastError = errors.length > 0 ? errors[errors.length - 1] : null;
		if (agent.errorCount >= 5) {
			await updateAgentStatus(agent.id, { status: "stopped" }, cwd);
			results.push({
				agentId: agent.id,
				recovered: false,
				action: "abandoned",
				reason: `Error count (${agent.errorCount}) exceeds threshold (5)`
			});
			continue;
		}
		if (lastError?.errorType === "permanent") {
			await updateAgentStatus(agent.id, { status: "stopped" }, cwd);
			results.push({
				agentId: agent.id,
				recovered: false,
				action: "abandoned",
				reason: `Permanent error: ${lastError.message}`
			});
			continue;
		}
		await updateAgentStatus(agent.id, { status: "starting" }, cwd);
		results.push({
			agentId: agent.id,
			recovered: true,
			action: "restarted",
			reason: "Agent reset to starting for re-assignment"
		});
	}
	return results;
}

export { recoverCrashedAgents };
//# sourceMappingURL=retry-Bq7dqsY8.js.map
