also looking at this
fix: report skipped gates as skipped, and honour ai.majorUpgrades.maxAttempts
#1433
9 files
+257
-8
| @@ -38,6 +38,16 @@ export interface UpgradeOptions { | ||
| 38 | 38 | autoMigrate?: boolean |
| 39 | 39 | /** Open as draft below this confidence (default: high) */ |
| 40 | 40 | draftBelowConfidence?: MigrationConfidence |
| 41 | /** | |
| 42 | * Agent runs allowed before the migration is reported as failed (default: 1). | |
| 43 | * | |
| 44 | * Declared on `ai.majorUpgrades` and documented, and the agent ran exactly | |
| 45 | * once whatever it said. A second attempt is handed the first one's output | |
| 46 | * so it continues rather than starting over. | |
| 47 | */ | |
| 48 | maxAttempts?: number | |
| 49 | /** The agent runner; injectable so attempts can be tested without a model */ | |
| 50 | agent?: typeof runAgent | |
| 41 | 51 | logger?: Logger |
| 42 | 52 | } |
| 43 | 53 | |
| @@ -116,9 +126,13 @@ export async function attemptMajorUpgrade(options: UpgradeOptions): Promise<Upgr | ||
| 116 | 126 | return { plan, report: renderMigrationReport(plan, undefined, usage), draft: false, status: 'analysis-only' } |
| 117 | 127 | } |
| 118 | 128 | |
| 119 | const result = await runAgent(options.ai, { | |
| 129 | const run = options.agent ?? runAgent | |
| 130 | const maxAttempts = Math.max(1, Math.floor(options.maxAttempts ?? 1)) | |
| 131 | const task = buildMigrationTask(plan) | |
| 132 | ||
| 133 | let result = await run(options.ai, { | |
| 120 | 134 | mode: implementMode, |
| 121 | task: buildMigrationTask(plan), | |
| 135 | task, | |
| 122 | 136 | context: { |
| 123 | 137 | workspace: options.workspace, |
| 124 | 138 | baseBranch: options.baseBranch, |
| @@ -126,11 +140,36 @@ export async function attemptMajorUpgrade(options: UpgradeOptions): Promise<Upgr | ||
| 126 | 140 | }, |
| 127 | 141 | logger, |
| 128 | 142 | }) |
| 143 | let attempts = 1 | |
| 144 | let applied = result.toolCalls > 0 | |
| 145 | ||
| 146 | // A run that stopped short — out of tool calls, out of time, gave up — is | |
| 147 | // handed its own output and asked to continue from the workspace as it now | |
| 148 | // stands. Starting over would redo the work that did land. | |
| 149 | while (result.stopReason !== 'completed' && attempts < maxAttempts) { | |
| 150 | attempts++ | |
| 151 | logger.info(`🔧 Migration attempt ${attempts} of ${maxAttempts} (previous run ended: ${result.stopReason})`) | |
| 152 | ||
| 153 | result = await run(options.ai, { | |
| 154 | mode: implementMode, | |
| 155 | task: `${task}\n\nA previous attempt ended (${result.stopReason}) with this output:\n\n${ | |
| 156 | result.output || '(no output)' | |
| 157 | }\n\nContinue from the workspace as it stands now. Do not redo work that is already done.`, | |
| 158 | context: { | |
| 159 | workspace: options.workspace, | |
| 160 | baseBranch: options.baseBranch, | |
| 161 | ...(options.branch ? { branch: options.branch } : {}), | |
| 162 | }, | |
| 163 | logger, | |
| 164 | }) | |
| 165 | applied = applied || result.toolCalls > 0 | |
| 166 | } | |
| 129 | 167 | |
| 130 | 168 | const verified = result.stopReason === 'completed' |
| 131 | 169 | const outcome: MigrationOutcome = { |
| 132 | applied: result.toolCalls > 0, | |
| 170 | applied, | |
| 133 | 171 | verified, |
| 172 | attempts, | |
| 134 | 173 | changedFiles: knownFiles, |
| 135 | 174 | unresolved: verified ? [] : plan.changes.filter(change => !change.automatable).map(change => change.action), |
| 136 | 175 | ...(verified ? {} : { verificationOutput: result.output }), |