import { s as pathExists } from "./absolute-path-CYFPfAjt.js";
import "./fs-safe-gVoExnQ2.js";
import { p as resolveAgentWorkspaceDir } from "./agent-scope-config-adSj-fCz.js";
import "./agent-scope-CB42Y_8g.js";
import { t as listGatewayAgentsBasic } from "./agent-list-B0FRl366.js";
import { n as readStatusSessionStores } from "./session-stores-DixHzBUk.js";
import path from "node:path";
//#region src/commands/status.agent-local.ts
/** Returns per-agent local workspace, bootstrap, session count, and last activity status. */
async function getAgentLocalStatuses(cfg) {
	const agentList = listGatewayAgentsBasic(cfg);
	const now = Date.now();
	const sessionStores = readStatusSessionStores(cfg, agentList.agents);
	const statuses = [];
	for (const { agent, path: sessionsPath, sessions } of sessionStores.byAgent) {
		const agentId = agent.id;
		const workspaceDir = (() => {
			try {
				return resolveAgentWorkspaceDir(cfg, agentId);
			} catch {
				return null;
			}
		})();
		const bootstrapPath = workspaceDir != null ? path.join(workspaceDir, "BOOTSTRAP.md") : null;
		const bootstrapPending = bootstrapPath != null ? await pathExists(bootstrapPath) : null;
		const lastUpdatedAt = sessions.reduce((max, session) => Math.max(max, session.entry.updatedAt ?? 0), 0);
		const resolvedLastUpdatedAt = lastUpdatedAt > 0 ? lastUpdatedAt : null;
		const lastActiveAgeMs = resolvedLastUpdatedAt ? now - resolvedLastUpdatedAt : null;
		statuses.push({
			id: agentId,
			name: agent.name,
			workspaceDir,
			bootstrapPending,
			sessionsPath,
			sessionsCount: sessions.length,
			lastUpdatedAt: resolvedLastUpdatedAt,
			lastActiveAgeMs
		});
	}
	const bootstrapPendingCount = statuses.reduce((sum, s) => sum + (s.bootstrapPending ? 1 : 0), 0);
	return {
		defaultId: agentList.selectionRequired ? null : agentList.defaultId,
		ownership: agentList.ownership ?? (agentList.selectionRequired === true ? "explicit" : "sole"),
		selectionRequired: agentList.selectionRequired === true,
		agents: statuses,
		totalSessions: sessionStores.sessions.length,
		bootstrapPendingCount
	};
}
//#endregion
export { getAgentLocalStatuses };
