|
| 1 | +/** |
| 2 | + * @fileoverview Resume command for Ralph Wiggum CLI. |
| 3 | + * Resumes a stopped loop with context about previous work. |
| 4 | + * @module commands/resume |
| 5 | + */ |
| 6 | +import { log } from "@clack/prompts"; |
| 7 | +import pc from "picocolors"; |
| 8 | + |
| 9 | +import { runLoop } from "../core/loop-runner"; |
| 10 | +import { LoopConfigSchema } from "../schemas/config"; |
| 11 | +import { type LoopState, stateFromJson } from "../schemas/state"; |
| 12 | +import { STATE_FILE } from "../utils/paths"; |
| 13 | + |
| 14 | +/** |
| 15 | + * CLI options for the resume command (raw string values from commander). |
| 16 | + */ |
| 17 | +interface ResumeOptions { |
| 18 | + /** Minimum iterations before checking completion (string from CLI) */ |
| 19 | + minIterations?: string; |
| 20 | + /** Maximum iterations, 0 for unlimited (string from CLI) */ |
| 21 | + maxIterations?: string; |
| 22 | + /** Phrase that signals loop completion */ |
| 23 | + completionPromise?: string; |
| 24 | + /** Optional agent name override */ |
| 25 | + agent?: string; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Resumes a stopped Ralph Wiggum loop. |
| 30 | + * Reads the existing state file, extracts information about previous work, |
| 31 | + * and continues the loop with enhanced context. |
| 32 | + * @param opts - Command options from CLI |
| 33 | + * @returns Resolves when the loop completes or is interrupted |
| 34 | + * @throws Exits process with code 1 if validation fails or no state file found |
| 35 | + */ |
| 36 | +export async function resumeCommand(opts: ResumeOptions): Promise<void> { |
| 37 | + const stateFile = Bun.file(STATE_FILE); |
| 38 | + |
| 39 | + // Check if state file exists |
| 40 | + if (!(await stateFile.exists())) { |
| 41 | + log.error( |
| 42 | + pc.red( |
| 43 | + "No stopped Ralph loop found. Run 'ralph loop' to start a new loop.", |
| 44 | + ), |
| 45 | + ); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + // Read and parse the existing state |
| 50 | + let existingState: LoopState; |
| 51 | + try { |
| 52 | + const content = await stateFile.text(); |
| 53 | + existingState = stateFromJson(content); |
| 54 | + } catch (error) { |
| 55 | + log.error(pc.red(`Failed to parse state file: ${error}`)); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + // Display information about the previous loop |
| 60 | + log.info(pc.bold(pc.blue("Resuming Ralph loop"))); |
| 61 | + log.message(` Previous iteration: ${existingState.iteration}`); |
| 62 | + log.message(` Original prompt: ${pc.dim(existingState.prompt)}`); |
| 63 | + if (existingState.previousFeedback?.qualityScore) { |
| 64 | + log.message( |
| 65 | + ` Last quality score: ${existingState.previousFeedback.qualityScore}/10`, |
| 66 | + ); |
| 67 | + } |
| 68 | + console.log(); |
| 69 | + |
| 70 | + // Build enhanced prompt with resume context |
| 71 | + const resumeContext = buildResumeContext(existingState); |
| 72 | + const enhancedPrompt = `${resumeContext}\n\nOriginal task: ${existingState.prompt}`; |
| 73 | + |
| 74 | + // Parse and validate options with Zod |
| 75 | + // Use existing state values as defaults if not provided |
| 76 | + const result = LoopConfigSchema.safeParse({ |
| 77 | + prompt: enhancedPrompt, |
| 78 | + minIterations: opts.minIterations |
| 79 | + ? Number.parseInt(opts.minIterations, 10) |
| 80 | + : existingState.minIterations, |
| 81 | + maxIterations: opts.maxIterations |
| 82 | + ? Number.parseInt(opts.maxIterations, 10) |
| 83 | + : existingState.maxIterations, |
| 84 | + completionPromise: |
| 85 | + opts.completionPromise ?? existingState.completionPromise, |
| 86 | + agentName: opts.agent ?? null, |
| 87 | + isResume: true, |
| 88 | + resumeFromIteration: existingState.iteration, |
| 89 | + }); |
| 90 | + |
| 91 | + if (!result.success) { |
| 92 | + // Format Zod error messages |
| 93 | + const errorMessages = result.error.issues |
| 94 | + .map((issue) => ` - ${issue.path.join(".")}: ${issue.message}`) |
| 95 | + .join("\n"); |
| 96 | + log.error(pc.red(`Validation error:\n${errorMessages}`)); |
| 97 | + process.exit(1); |
| 98 | + } |
| 99 | + |
| 100 | + await runLoop(result.data); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Builds context text about the previous loop for the resume prompt. |
| 105 | + * @param state - The existing loop state |
| 106 | + * @returns Formatted context string |
| 107 | + */ |
| 108 | +function buildResumeContext(state: ReturnType<typeof stateFromJson>): string { |
| 109 | + const lines = [ |
| 110 | + "RESUME CONTEXT:", |
| 111 | + "===============", |
| 112 | + `You are resuming a Ralph loop that was stopped at iteration ${state.iteration}.`, |
| 113 | + "", |
| 114 | + "Before continuing, please:", |
| 115 | + "1. Review what was accomplished in previous iterations by checking:", |
| 116 | + " - Files that were created or modified", |
| 117 | + " - Git history (git log, git diff)", |
| 118 | + " - Test results", |
| 119 | + " - Build artifacts", |
| 120 | + "", |
| 121 | + "2. Review the previous feedback to understand where you left off:", |
| 122 | + ]; |
| 123 | + |
| 124 | + if (state.previousFeedback) { |
| 125 | + if (state.previousFeedback.qualitySummary) { |
| 126 | + lines.push(` Quality: ${state.previousFeedback.qualitySummary}`); |
| 127 | + } |
| 128 | + |
| 129 | + if ( |
| 130 | + state.previousFeedback.nextSteps && |
| 131 | + state.previousFeedback.nextSteps.length > 0 |
| 132 | + ) { |
| 133 | + lines.push(" Planned next steps:"); |
| 134 | + for (const step of state.previousFeedback.nextSteps) { |
| 135 | + lines.push(` - ${step}`); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if ( |
| 140 | + state.previousFeedback.improvements && |
| 141 | + state.previousFeedback.improvements.length > 0 |
| 142 | + ) { |
| 143 | + lines.push(" Areas for improvement:"); |
| 144 | + for (const improvement of state.previousFeedback.improvements) { |
| 145 | + lines.push(` - ${improvement}`); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if ( |
| 150 | + state.previousFeedback.blockers && |
| 151 | + state.previousFeedback.blockers.length > 0 |
| 152 | + ) { |
| 153 | + lines.push(" Blockers:"); |
| 154 | + for (const blocker of state.previousFeedback.blockers) { |
| 155 | + lines.push(` - ${blocker}`); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if ( |
| 160 | + state.previousFeedback.ideas && |
| 161 | + state.previousFeedback.ideas.length > 0 |
| 162 | + ) { |
| 163 | + lines.push(" Ideas to consider:"); |
| 164 | + for (const idea of state.previousFeedback.ideas) { |
| 165 | + lines.push(` - ${idea}`); |
| 166 | + } |
| 167 | + } |
| 168 | + } else { |
| 169 | + lines.push(" (No previous feedback available)"); |
| 170 | + } |
| 171 | + |
| 172 | + lines.push(""); |
| 173 | + lines.push( |
| 174 | + "3. Continue working toward completion of the original task below.", |
| 175 | + ); |
| 176 | + lines.push(""); |
| 177 | + |
| 178 | + return lines.join("\n"); |
| 179 | +} |
0 commit comments