/** * add_phase tool * * Agent tool to add a new phase to the current mission run. * Auto-generates phase_id ("phase1", "phase2", ...). * Sets status to "pending". * * Used by: mission-pm skill */ import { readState, readRun, writeRun, generatePhaseId, createPhase, addPhaseToRun } from "../state.js"; export interface AddPhaseParams { name: string; file: string; } export interface AddPhaseResult { success: boolean; message: string; phaseId: string | null; errors: string[]; } /** * Add a phase to the active mission run * * @param params.name - Human-readable phase name (e.g., "Auth Backend") * @param params.file - Path to phase definition/reference file * @returns Result with generated phase_id */ export function addPhase(params: AddPhaseParams): AddPhaseResult { const result: AddPhaseResult = { success: false, message: "", phaseId: null, errors: [] }; try { // Validate parameters if (!params.name || params.name.trim() === "") { result.errors.push("Phase name is required"); result.message = "Failed to add phase: name is required"; return result; } if (!params.file || params.file.trim() === "") { result.errors.push("Phase file is required"); result.message = "Failed to add phase: file is required"; return result; } // Get active run const state = readState(); if (!state.active_run_id) { result.errors.push("No active run"); result.message = "Failed to add phase: no active mission run"; return result; } // Read run.json const run = readRun(state.active_run_id); if (!run) { result.errors.push(`Run not found: ${state.active_run_id}`); result.message = "Failed to add phase: run not found"; return result; } // Generate phase ID const phaseId = generatePhaseId(run); // Create phase const phase = createPhase(phaseId, params.name.trim(), params.file.trim()); // Add to run const updatedRun = addPhaseToRun(run, phase); writeRun(updatedRun); result.success = true; result.phaseId = phaseId; result.message = `Phase added: ${phaseId} - ${params.name}`; } catch (error) { result.success = false; result.message = `Error adding phase: ${error instanceof Error ? error.message : String(error)}`; result.errors.push(String(error)); } return result; }