(run, deps)
| 86 | * }} deps |
| 87 | */ |
| 88 | export async function runAutopilot(run, deps) { |
| 89 | const onProgress = deps.onProgress || (() => {}); |
| 90 | let lastKey = null; |
| 91 | try { |
| 92 | while (true) { |
| 93 | if (run.cancelled) { |
| 94 | run.status = "cancelled"; |
| 95 | break; |
| 96 | } |
| 97 | if (run.completed.length >= run.maxSteps) { |
| 98 | run.status = "capped"; |
| 99 | break; |
| 100 | } |
| 101 | const state = await deps.snapshot(); |
| 102 | const step = selectNextStep(state); |
| 103 | if (!step) { |
| 104 | run.status = "completed"; |
| 105 | break; |
| 106 | } |
| 107 | // Phase scope: stop once the active phase advances past where we began. |
| 108 | if (run.scope === "phase" && run.startRank != null && step.rank != null && step.rank > run.startRank) { |
| 109 | run.status = "phase_done"; |
| 110 | break; |
| 111 | } |
| 112 | const key = stepKey(step); |
| 113 | // Selecting the same step twice running means the previous attempt did |
| 114 | // not check it off — the agent is stuck or waiting on a decision. Stop |
| 115 | // and hand control back rather than loop on it. |
| 116 | if (key === lastKey) { |
| 117 | run.status = "stuck"; |
| 118 | run.stuck = step.title; |
| 119 | break; |
| 120 | } |
| 121 | lastKey = key; |
| 122 | |
| 123 | run.current = { title: step.title, section: step.section || null }; |
| 124 | onProgress(run, state); |
| 125 | if (deps.log) deps.log("Autopilot → " + step.title, { ephemeral: true }); |
| 126 | |
| 127 | let stepError = null; |
| 128 | try { |
| 129 | await deps.runTurn(deps.buildStepPrompt(step)); |
| 130 | } catch (e) { |
| 131 | stepError = (e && e.message) || String(e); |
| 132 | } |
| 133 | |
| 134 | const after = await deps.snapshot(); |
| 135 | const done = isStepDone(after, step); |
| 136 | run.completed.push({ |
| 137 | title: step.title, |
| 138 | section: step.section || null, |
| 139 | done, |
| 140 | error: stepError, |
| 141 | at: Date.now(), |
| 142 | }); |
| 143 | run.current = null; |
| 144 | onProgress(run, after); |
| 145 |
no test coverage detected