|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | + |
| 3 | +/** |
| 4 | + * Test script to verify Ralph loop can iterate beyond 5 iterations |
| 5 | + */ |
| 6 | + |
| 7 | +import * as fs from 'fs/promises'; |
| 8 | +import * as path from 'path'; |
| 9 | +import { existsSync } from 'fs'; |
| 10 | + |
| 11 | +const RALPH_DIR = '.ralph'; |
| 12 | +const MAX_TEST_ITERATIONS = 10; // Test up to 10 iterations |
| 13 | + |
| 14 | +interface RalphLoopState { |
| 15 | + loopId?: string; |
| 16 | + task: string; |
| 17 | + criteria?: string; |
| 18 | + iteration: number; |
| 19 | + status: string; |
| 20 | + startTime: number; |
| 21 | +} |
| 22 | + |
| 23 | +async function ensureDirectory(dir: string): Promise<void> { |
| 24 | + if (!existsSync(dir)) { |
| 25 | + await fs.mkdir(dir, { recursive: true }); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +async function saveLoopState(state: RalphLoopState): Promise<void> { |
| 30 | + // Save state.json |
| 31 | + await fs.writeFile( |
| 32 | + path.join(RALPH_DIR, 'state.json'), |
| 33 | + JSON.stringify(state, null, 2) |
| 34 | + ); |
| 35 | + |
| 36 | + // Synchronize iteration.txt with current iteration |
| 37 | + await fs.writeFile( |
| 38 | + path.join(RALPH_DIR, 'iteration.txt'), |
| 39 | + state.iteration.toString() |
| 40 | + ); |
| 41 | + |
| 42 | + console.log(`✅ Saved state for iteration ${state.iteration}`); |
| 43 | +} |
| 44 | + |
| 45 | +async function simulateIteration(iterationNum: number): Promise<void> { |
| 46 | + const historyDir = path.join( |
| 47 | + RALPH_DIR, |
| 48 | + 'history', |
| 49 | + `iteration-${iterationNum.toString().padStart(3, '0')}` |
| 50 | + ); |
| 51 | + await ensureDirectory(historyDir); |
| 52 | + |
| 53 | + // Create artifacts for this iteration |
| 54 | + const artifacts = { |
| 55 | + analysis: { |
| 56 | + filesCount: Math.floor(Math.random() * 10) + 1, |
| 57 | + testsPass: Math.floor(Math.random() * 20), |
| 58 | + testsFail: Math.floor(Math.random() * 5), |
| 59 | + lastChange: `Iteration ${iterationNum} changes`, |
| 60 | + }, |
| 61 | + plan: { |
| 62 | + summary: `Work for iteration ${iterationNum}`, |
| 63 | + steps: [ |
| 64 | + `Task ${iterationNum}-1`, |
| 65 | + `Task ${iterationNum}-2`, |
| 66 | + `Task ${iterationNum}-3`, |
| 67 | + ], |
| 68 | + priority: 'high', |
| 69 | + }, |
| 70 | + changes: [ |
| 71 | + { |
| 72 | + step: `Task ${iterationNum}-1`, |
| 73 | + timestamp: Date.now(), |
| 74 | + result: 'completed', |
| 75 | + }, |
| 76 | + ], |
| 77 | + validation: { |
| 78 | + testsPass: true, |
| 79 | + lintClean: true, |
| 80 | + errors: [], |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + await fs.writeFile( |
| 85 | + path.join(historyDir, 'artifacts.json'), |
| 86 | + JSON.stringify(artifacts, null, 2) |
| 87 | + ); |
| 88 | + |
| 89 | + console.log(`📝 Created artifacts for iteration ${iterationNum}`); |
| 90 | +} |
| 91 | + |
| 92 | +async function runIterationTest(): Promise<void> { |
| 93 | + console.log('🎭 Starting Ralph iteration test...'); |
| 94 | + console.log(`🎯 Goal: Test ${MAX_TEST_ITERATIONS} iterations\n`); |
| 95 | + |
| 96 | + // Ensure directories exist |
| 97 | + await ensureDirectory(RALPH_DIR); |
| 98 | + await ensureDirectory(path.join(RALPH_DIR, 'history')); |
| 99 | + |
| 100 | + // Initialize state |
| 101 | + const initialState: RalphLoopState = { |
| 102 | + task: 'Test multiple iterations beyond 5', |
| 103 | + iteration: 0, |
| 104 | + status: 'initialized', |
| 105 | + startTime: Date.now(), |
| 106 | + }; |
| 107 | + |
| 108 | + await saveLoopState(initialState); |
| 109 | + |
| 110 | + // Run iterations |
| 111 | + for (let i = 0; i < MAX_TEST_ITERATIONS; i++) { |
| 112 | + console.log(`\n--- Iteration ${i} ---`); |
| 113 | + |
| 114 | + // Simulate iteration work |
| 115 | + await simulateIteration(i); |
| 116 | + |
| 117 | + // Update state |
| 118 | + const state: RalphLoopState = { |
| 119 | + task: 'Test multiple iterations beyond 5', |
| 120 | + iteration: i + 1, |
| 121 | + status: 'running', |
| 122 | + startTime: initialState.startTime, |
| 123 | + }; |
| 124 | + |
| 125 | + await saveLoopState(state); |
| 126 | + |
| 127 | + // Check if we're past the old limit |
| 128 | + if (i === 5) { |
| 129 | + console.log('\n🎉 Successfully passed iteration 5!'); |
| 130 | + } |
| 131 | + |
| 132 | + // Small delay to simulate work |
| 133 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 134 | + } |
| 135 | + |
| 136 | + console.log('\n✅ Test completed successfully!'); |
| 137 | + console.log(`📊 Final iteration count: ${MAX_TEST_ITERATIONS}`); |
| 138 | + |
| 139 | + // Verify final state |
| 140 | + const finalIteration = await fs.readFile( |
| 141 | + path.join(RALPH_DIR, 'iteration.txt'), |
| 142 | + 'utf8' |
| 143 | + ); |
| 144 | + |
| 145 | + const finalState = JSON.parse( |
| 146 | + await fs.readFile(path.join(RALPH_DIR, 'state.json'), 'utf8') |
| 147 | + ); |
| 148 | + |
| 149 | + console.log(`\n📋 Final verification:`); |
| 150 | + console.log(` iteration.txt: ${finalIteration}`); |
| 151 | + console.log(` state.json iteration: ${finalState.iteration}`); |
| 152 | + |
| 153 | + if (parseInt(finalIteration) === MAX_TEST_ITERATIONS) { |
| 154 | + console.log('\n🎊 SUCCESS: Ralph loop can iterate beyond 5 iterations!'); |
| 155 | + } else { |
| 156 | + console.error('\n❌ FAILURE: Iteration count mismatch'); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// Run the test |
| 161 | +runIterationTest().catch((error) => { |
| 162 | + console.error('❌ Test failed:', error); |
| 163 | + process.exit(1); |
| 164 | +}); |
0 commit comments