| 58 | * Always terminates. Never recurses. Fully traced. |
| 59 | */ |
| 60 | export async function runLoop<T>(config: LoopConfig<T>): Promise<LoopResult<T>> { |
| 61 | const startMs = Date.now(); |
| 62 | const state: LoopState<T> = { current: null, feedback: null, history: [] }; |
| 63 | const iterationHistory: LoopResult<T>["history"] = []; |
| 64 | let bestOutput: T | null = null; |
| 65 | let bestIteration = 0; |
| 66 | let bestValid = false; |
| 67 | |
| 68 | for (let i = 0; i < config.max_iterations; i++) { |
| 69 | const iterStart = Date.now(); |
| 70 | |
| 71 | // Execute step |
| 72 | let output: T; |
| 73 | try { |
| 74 | output = await config.step(state, i); |
| 75 | } catch (e: any) { |
| 76 | logger.error("loop.step_error", { loop: config.name, iteration: i, error: e.message }); |
| 77 | iterationHistory.push({ iteration: i, valid: false, duration_ms: Date.now() - iterStart, feedback: e.message }); |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | // Validate |
| 82 | let valid: boolean; |
| 83 | try { |
| 84 | valid = await config.validate(output); |
| 85 | } catch { |
| 86 | valid = false; |
| 87 | } |
| 88 | |
| 89 | // Track best |
| 90 | if (valid || !bestOutput) { |
| 91 | bestOutput = output; |
| 92 | bestIteration = i; |
| 93 | bestValid = valid; |
| 94 | } |
| 95 | |
| 96 | // Generate feedback if needed |
| 97 | let feedback: string | null = null; |
| 98 | if (!valid && config.critique) { |
| 99 | try { |
| 100 | feedback = await config.critique(output, i); |
| 101 | } catch { feedback = "Validation failed, please try again."; } |
| 102 | } |
| 103 | |
| 104 | // Update state |
| 105 | state.current = output; |
| 106 | state.feedback = feedback; |
| 107 | state.history.push({ output, valid, feedback }); |
| 108 | |
| 109 | const iterMs = Date.now() - iterStart; |
| 110 | iterationHistory.push({ iteration: i, valid, duration_ms: iterMs, feedback }); |
| 111 | |
| 112 | // Trace |
| 113 | counter("cognition.loop.iteration", { loop: config.name, iteration: String(i), valid: String(valid) }); |
| 114 | histogram("cognition.loop.iteration_ms", iterMs, { loop: config.name }); |
| 115 | |
| 116 | // Short-circuit on success |
| 117 | if (valid) { |