* Run a bounded refinement loop. * * Executes `step()` up to `max_iterations` times. After each step, * calls `validate()`. If valid, returns immediately. If not, optionally * calls `critique()` to generate feedback for the next iteration. * * Always terminates. Never recurses. Fully traced.
(config)
| 22 | * Always terminates. Never recurses. Fully traced. |
| 23 | */ |
| 24 | async function runLoop(config) { |
| 25 | const startMs = Date.now(); |
| 26 | const state = { current: null, feedback: null, history: [] }; |
| 27 | const iterationHistory = []; |
| 28 | let bestOutput = null; |
| 29 | let bestIteration = 0; |
| 30 | let bestValid = false; |
| 31 | for (let i = 0; i < config.max_iterations; i++) { |
| 32 | const iterStart = Date.now(); |
| 33 | // Execute step |
| 34 | let output; |
| 35 | try { |
| 36 | output = await config.step(state, i); |
| 37 | } |
| 38 | catch (e) { |
| 39 | logger_1.logger.error("loop.step_error", { loop: config.name, iteration: i, error: e.message }); |
| 40 | iterationHistory.push({ iteration: i, valid: false, duration_ms: Date.now() - iterStart, feedback: e.message }); |
| 41 | continue; |
| 42 | } |
| 43 | // Validate |
| 44 | let valid; |
| 45 | try { |
| 46 | valid = await config.validate(output); |
| 47 | } |
| 48 | catch { |
| 49 | valid = false; |
| 50 | } |
| 51 | // Track best |
| 52 | if (valid || !bestOutput) { |
| 53 | bestOutput = output; |
| 54 | bestIteration = i; |
| 55 | bestValid = valid; |
| 56 | } |
| 57 | // Generate feedback if needed |
| 58 | let feedback = null; |
| 59 | if (!valid && config.critique) { |
| 60 | try { |
| 61 | feedback = await config.critique(output, i); |
| 62 | } |
| 63 | catch { |
| 64 | feedback = "Validation failed, please try again."; |
| 65 | } |
| 66 | } |
| 67 | // Update state |
| 68 | state.current = output; |
| 69 | state.feedback = feedback; |
| 70 | state.history.push({ output, valid, feedback }); |
| 71 | const iterMs = Date.now() - iterStart; |
| 72 | iterationHistory.push({ iteration: i, valid, duration_ms: iterMs, feedback }); |
| 73 | // Trace |
| 74 | (0, metrics_1.counter)("cognition.loop.iteration", { loop: config.name, iteration: String(i), valid: String(valid) }); |
| 75 | (0, metrics_1.histogram)("cognition.loop.iteration_ms", iterMs, { loop: config.name }); |
| 76 | // Short-circuit on success |
| 77 | if (valid) { |
| 78 | logger_1.logger.info("loop.success", { loop: config.name, iterations: i + 1, total_ms: Date.now() - startMs }); |
| 79 | break; |
| 80 | } |
| 81 | } |
no test coverage detected