/** * mission_resume tool * * Agent tool to resume a previously created mission run. * Sets state.json active_run_id to the specified run_id. * * Used by: mission-orchestrator skill (when user asks to resume) */ import { readState, readRun, updateState, RunStatus } from "../state.js"; export interface MissionResumeParams { run_id: string; phase?: string; statusMessage?: string; } export interface MissionResumeResult { success: boolean; message: string; runId: string | null; runStatus: RunStatus | null; previousActiveRunId: string | null; errors: string[]; } /** * Resume a mission run * * Sets the specified run as the active run in state.json. * Fails if there's already a different active run. * * @param params.run_id - ID of the run to resume * @param params.phase - Optional phase to set as current (defaults to run's current state) * @param params.statusMessage - Optional status message * @returns Result with resume info */ export function missionResume(params: MissionResumeParams): MissionResumeResult { const result: MissionResumeResult = { success: false, message: "", runId: null, runStatus: null, previousActiveRunId: null, errors: [] }; try { // Validate parameters if (!params.run_id || params.run_id.trim() === "") { result.errors.push("Run ID is required"); result.message = "Failed to resume mission: run_id is required"; return result; } const runId = params.run_id.trim(); result.runId = runId; // Read current state const state = readState(); result.previousActiveRunId = state.active_run_id; // Check if there's already a different active run if (state.active_run_id && state.active_run_id !== runId) { result.errors.push(`Another run is active: ${state.active_run_id}. Complete or pause it first.`); result.message = "Failed to resume mission: another run is active"; return result; } // Read the run to verify it exists const run = readRun(runId); if (!run) { result.errors.push(`Run not found: ${runId}`); result.message = "Failed to resume mission: run not found"; return result; } result.runStatus = run.status; // Check if run is in a resumable state if (run.status === "done") { result.errors.push(`Run ${runId} is already complete`); result.message = "Failed to resume mission: run is already complete"; return result; } // Determine current phase let currentPhase = params.phase || "execution"; if (!params.phase && run.phases.length > 0) { // Find the first non-done phase const activePhase = run.phases.find(p => p.status !== "done" && p.status !== "removed"); if (activePhase) { currentPhase = activePhase.id; } } // Update state.json updateState({ active_run_id: runId, current_phase: currentPhase, current_status_message: params.statusMessage || `Resumed mission: ${runId}` }); result.success = true; result.message = `Mission resumed: ${runId}`; } catch (error) { result.success = false; result.message = `Error resuming mission: ${error instanceof Error ? error.message : String(error)}`; result.errors.push(String(error)); } return result; }