
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="7d652c3d-6de6-59c3-9f83-f7d09f874099")}catch(e){}}();
import { updateTaskDependencies } from "../core/task-board-mutations.js";
import { listWorkspaceIndexEntries, loadWorkspaceState, saveWorkspaceState } from "../state/workspace-state.js";
import { deleteTaskWorktree } from "../workspace/task-worktree.js";
import { collectProjectWorktreeTaskIdsForRemoval } from "./workspace-registry.js";
function moveTaskToTrash(board, taskId) {
    const columns = board.columns.map((column) => ({
        ...column,
        cards: [...column.cards],
    }));
    let removedCard;
    for (const column of columns) {
        const cardIndex = column.cards.findIndex((candidate) => candidate.id === taskId);
        if (cardIndex === -1) {
            continue;
        }
        removedCard = column.cards[cardIndex];
        column.cards.splice(cardIndex, 1);
        break;
    }
    if (!removedCard) {
        return board;
    }
    const trashColumnIndex = columns.findIndex((column) => column.id === "trash");
    if (trashColumnIndex === -1) {
        return board;
    }
    const trashColumn = columns[trashColumnIndex];
    if (!trashColumn.cards.some((candidate) => candidate.id === taskId)) {
        trashColumn.cards.unshift({
            ...removedCard,
            updatedAt: Date.now(),
        });
    }
    return updateTaskDependencies({
        ...board,
        columns,
    });
}
async function persistInterruptedSessions(workspacePath, interruptedTaskIds, options) {
    if (interruptedTaskIds.length === 0) {
        return [];
    }
    const workspaceState = options?.workspaceState ?? (await loadWorkspaceState(workspacePath));
    const worktreeTaskIds = collectProjectWorktreeTaskIdsForRemoval(workspaceState.board);
    const worktreeTaskIdsToCleanup = interruptedTaskIds.filter((taskId) => worktreeTaskIds.has(taskId));
    let nextBoard = workspaceState.board;
    for (const taskId of interruptedTaskIds) {
        nextBoard = moveTaskToTrash(nextBoard, taskId);
    }
    const nextSessions = {
        ...workspaceState.sessions,
    };
    for (const taskId of interruptedTaskIds) {
        const summary = options?.resolveSummary?.(taskId) ?? workspaceState.sessions[taskId] ?? null;
        if (summary) {
            nextSessions[taskId] = {
                ...summary,
                state: "interrupted",
                reviewReason: "interrupted",
                pid: null,
                updatedAt: Date.now(),
            };
        }
    }
    await saveWorkspaceState(workspacePath, {
        board: nextBoard,
        sessions: nextSessions,
    });
    return worktreeTaskIdsToCleanup;
}
async function cleanupInterruptedTaskWorktrees(repoPath, taskIds, warn) {
    if (taskIds.length === 0) {
        return;
    }
    const deletions = await Promise.all(taskIds.map(async (taskId) => ({
        taskId,
        deleted: await deleteTaskWorktree({
            repoPath,
            taskId,
        }),
    })));
    for (const { taskId, deleted } of deletions) {
        if (deleted.ok) {
            continue;
        }
        const message = deleted.error ?? `Could not delete task workspace for task "${taskId}" during shutdown.`;
        warn(message);
    }
}
function shouldInterruptSessionOnShutdown(summary) {
    if (summary.state === "running") {
        return true;
    }
    return summary.state === "awaiting_review";
}
function collectShutdownInterruptedTaskIds(interruptedSummaries, terminalManager) {
    const taskIds = new Set(interruptedSummaries.map((summary) => summary.taskId));
    for (const summary of terminalManager.listSummaries()) {
        if (!shouldInterruptSessionOnShutdown(summary)) {
            continue;
        }
        taskIds.add(summary.taskId);
    }
    return Array.from(taskIds);
}
function collectWorkColumnTaskIds(workspaceState) {
    return Array.from(collectProjectWorktreeTaskIdsForRemoval(workspaceState.board));
}
export async function shutdownRuntimeServer(deps) {
    if (deps.skipSessionCleanup) {
        await deps.closeRuntimeServer();
        return;
    }
    const interruptedByWorkspace = [];
    const managedWorkspacePaths = new Set();
    for (const { workspacePath, terminalManager } of deps.workspaceRegistry.listManagedWorkspaces()) {
        const interrupted = terminalManager.markInterruptedAndStopAll();
        const interruptedTaskIds = new Set(collectShutdownInterruptedTaskIds(interrupted, terminalManager));
        if (!workspacePath) {
            continue;
        }
        managedWorkspacePaths.add(workspacePath);
        try {
            const workspaceState = await loadWorkspaceState(workspacePath);
            for (const taskId of collectWorkColumnTaskIds(workspaceState)) {
                interruptedTaskIds.add(taskId);
            }
            interruptedByWorkspace.push({
                workspacePath,
                interruptedTaskIds: Array.from(interruptedTaskIds),
                workspaceState,
                resolveSummary: (taskId) => terminalManager.getSummary(taskId),
            });
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            deps.warn(`Could not load workspace state for ${workspacePath} during shutdown cleanup. ${message}`);
        }
    }
    const indexedWorkspaces = await listWorkspaceIndexEntries();
    for (const workspace of indexedWorkspaces) {
        if (managedWorkspacePaths.has(workspace.repoPath)) {
            continue;
        }
        try {
            const workspaceState = await loadWorkspaceState(workspace.repoPath);
            const interruptedTaskIds = collectWorkColumnTaskIds(workspaceState);
            if (interruptedTaskIds.length === 0) {
                continue;
            }
            interruptedByWorkspace.push({
                workspacePath: workspace.repoPath,
                interruptedTaskIds,
                workspaceState,
            });
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            deps.warn(`Could not load workspace state for ${workspace.repoPath} during shutdown cleanup. ${message}`);
        }
    }
    await Promise.all(interruptedByWorkspace.map(async (workspace) => {
        const worktreeTaskIds = await persistInterruptedSessions(workspace.workspacePath, workspace.interruptedTaskIds, {
            workspaceState: workspace.workspaceState,
            resolveSummary: workspace.resolveSummary,
        });
        await cleanupInterruptedTaskWorktrees(workspace.workspacePath, worktreeTaskIds, deps.warn);
    }));
    await deps.closeRuntimeServer();
}
//# sourceMappingURL=shutdown-coordinator.js.map
//# debugId=7d652c3d-6de6-59c3-9f83-f7d09f874099
