From 2332f6491ebdabb4be65306f11955e65c6aef88d Mon Sep 17 00:00:00 2001 From: onoderakosen89 <72384345+onoderakosen89@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:29:56 -0500 Subject: [PATCH 1/3] Specify the independent check for #3 --- test/repair-loop.test.mjs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/repair-loop.test.mjs b/test/repair-loop.test.mjs index 44f9ee1..ff02789 100644 --- a/test/repair-loop.test.mjs +++ b/test/repair-loop.test.mjs @@ -126,3 +126,35 @@ test('a modified diff blocks repair and a failed post-diagnosis repair cannot lo assert.equal(result.status, mutateDuringDiagnosis ? 'diagnosis_changed_diff' : 'repair_limit'); } }); + +test('operational failures keep their stage and the evidence already collected', async () => { + for (const scenario of ['initial_check', 'recheck', 'read_diff', 'repair_and_diff']) { + let edits = 0; + let reads = 0; + const unavailable = new Error('fixture-private-raw-error'); + const result = await runRepairLoop({ + check: async ({ id }) => { + if (scenario === 'initial_check' || (scenario === 'recheck' && edits)) throw unavailable; + return { observation: { id, outcome: 'failure' }, evidence: ['recorded assertion'] }; + }, + repair: async () => { + edits++; + if (scenario === 'repair_and_diff') throw unavailable; + }, + readDiff: async () => { + reads++; + if (scenario === 'read_diff' || (scenario === 'repair_and_diff' && reads > 1)) throw unavailable; + return edits ? 'changed' : 'known baseline'; + }, + }); + assert.ok(result.consultation, scenario); + assert.equal(result.consultation.stage, scenario === 'repair_and_diff' ? 'repair' : scenario); + assert.ok(!JSON.stringify(result).includes(unavailable.message), scenario); + if (scenario === 'recheck' || scenario === 'repair_and_diff') { + assert.deepEqual(result.consultation.latest.evidence, ['recorded assertion']); + assert.equal(result.consultation.attempts.length, 1); + assert.equal(result.consultation.diff, scenario === 'recheck' ? 'changed' : 'known baseline'); + } + if (scenario === 'read_diff') assert.equal(edits, 0); + } +}); From c0245119ac3bc9792d3d5c6b097f8375f345da22 Mon Sep 17 00:00:00 2001 From: onoderakosen89 <72384345+onoderakosen89@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:30:33 -0500 Subject: [PATCH 2/3] Assert the expected escaped callback rejection --- test/repair-loop.test.mjs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/test/repair-loop.test.mjs b/test/repair-loop.test.mjs index ff02789..b843342 100644 --- a/test/repair-loop.test.mjs +++ b/test/repair-loop.test.mjs @@ -132,21 +132,27 @@ test('operational failures keep their stage and the evidence already collected', let edits = 0; let reads = 0; const unavailable = new Error('fixture-private-raw-error'); - const result = await runRepairLoop({ - check: async ({ id }) => { - if (scenario === 'initial_check' || (scenario === 'recheck' && edits)) throw unavailable; - return { observation: { id, outcome: 'failure' }, evidence: ['recorded assertion'] }; - }, - repair: async () => { - edits++; - if (scenario === 'repair_and_diff') throw unavailable; - }, - readDiff: async () => { - reads++; - if (scenario === 'read_diff' || (scenario === 'repair_and_diff' && reads > 1)) throw unavailable; - return edits ? 'changed' : 'known baseline'; - }, - }); + let result; + try { + result = await runRepairLoop({ + check: async ({ id }) => { + if (scenario === 'initial_check' || (scenario === 'recheck' && edits)) throw unavailable; + return { observation: { id, outcome: 'failure' }, evidence: ['recorded assertion'] }; + }, + repair: async () => { + edits++; + if (scenario === 'repair_and_diff') throw unavailable; + }, + readDiff: async () => { + reads++; + if (scenario === 'read_diff' || (scenario === 'repair_and_diff' && reads > 1)) throw unavailable; + return edits ? 'changed' : 'known baseline'; + }, + }); + } catch (error) { + if (error !== unavailable) throw error; + assert.fail(`${scenario}: the known callback rejection escaped instead of returning consultation`); + } assert.ok(result.consultation, scenario); assert.equal(result.consultation.stage, scenario === 'repair_and_diff' ? 'repair' : scenario); assert.ok(!JSON.stringify(result).includes(unavailable.message), scenario); From 3eb296b9facdf2b8e70d8a8d4d26bdbb9243eed7 Mon Sep 17 00:00:00 2001 From: onoderakosen89 <72384345+onoderakosen89@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:31:58 -0500 Subject: [PATCH 3/3] Preserve diagnostic evidence when a repair-loop callback throws --- src/repair-loop.mjs | 70 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/src/repair-loop.mjs b/src/repair-loop.mjs index 8646b7e..66726e8 100644 --- a/src/repair-loop.mjs +++ b/src/repair-loop.mjs @@ -19,6 +19,13 @@ const questions = { no_change: 'What prevented a code change, and does the task need a decision before another attempt?', }; +const operationalErrors = { + initial_check: 'The initial check callback failed.', + repair: 'The repair callback failed.', + recheck: 'The recheck callback failed.', + read_diff: 'The diff reader callback failed.', +}; + /** The caller supplies a trusted check, a repair session, and a project diff reader. */ export async function runRepairLoop({ check, repair, readDiff, maxRepairs = 2, diagnose }) { if (!Number.isSafeInteger(maxRepairs) || maxRepairs < 1) { @@ -27,16 +34,19 @@ export async function runRepairLoop({ check, repair, readDiff, maxRepairs = 2, d if (diagnose !== undefined && typeof diagnose !== 'function') { throw new TypeError('diagnose must be a function'); } + for (const [name, callback] of Object.entries({ check, repair, readDiff })) { + if (typeof callback !== 'function') throw new TypeError(`${name} must be a function`); + } const attempts = []; let diagnosis; - const initial = await check({ id: 'before' }); - let latest = initial; - let diff = await readDiff(); + let initial; + let latest; + let diff; - function result(status) { + function result(status, stage) { const run = { status, initial, attempts, latest, ...(diagnosis && { diagnosis }) }; if (status === 'passed' || status === 'already_passed') return run; - const question = latest.reason === 'regression_test_changed' + const question = latest?.reason === 'regression_test_changed' ? 'Can the original independent check be restored before resuming?' : (status === 'diagnosis_unknown' || status === 'diagnosis_needs_decision' ? diagnosis.next : questions[status]); @@ -45,10 +55,25 @@ export async function runRepairLoop({ check, repair, readDiff, maxRepairs = 2, d : stopSummary[status]; return { ...run, - consultation: { reason: status, summary, question, latest, diff, attempts }, + consultation: { + reason: status, summary, question, latest, diff, attempts, + ...(stage && { stage, error: operationalErrors[stage] }), + }, }; } + try { + initial = await check({ id: 'before' }); + } catch { + return result('unknown', 'initial_check'); + } + latest = initial; + try { + diff = await readDiff(); + } catch { + return result('unknown', 'read_diff'); + } + if (initial.observation.outcome === 'success') return result('already_passed'); if (initial.observation.outcome !== 'failure') return result('unknown'); @@ -60,22 +85,33 @@ export async function runRepairLoop({ check, repair, readDiff, maxRepairs = 2, d try { usage = await repair(problem); } catch { - diff = await readDiff(); attempts.push({ number, interrupted: true }); - return result('repair_interrupted'); + try { + diff = await readDiff(); + } catch { + // Keep the last known diff; the repair failure is primary. + } + return result('repair_interrupted', 'repair'); + } + const previousDiff = diff; + try { + diff = await readDiff(); + } catch { + attempts.push({ number, usage }); + return result('unknown', 'read_diff'); } - const changed = await readDiff(); const previousFailure = latest.observation.id; - latest = await check({ id: `after-${number}`, resolves: previousFailure }); + try { + latest = await check({ id: `after-${number}`, resolves: previousFailure }); + } catch { + attempts.push({ number, usage }); + return result('unknown', 'recheck'); + } attempts.push({ number, usage, ...latest }); if (latest.observation.outcome === 'success') return result('passed'); - if (latest.observation.outcome !== 'failure') { - diff = changed; - return result('unknown'); - } - const hasChange = changed !== diff; - diff = changed; + if (latest.observation.outcome !== 'failure') return result('unknown'); + const hasChange = diff !== previousDiff; if (diagnose && number === maxRepairs) { let decision; try { @@ -87,7 +123,7 @@ export async function runRepairLoop({ check, repair, readDiff, maxRepairs = 2, d try { afterDiagnosis = await readDiff(); } catch { - return result('diagnosis_diff_unavailable'); + return result('diagnosis_diff_unavailable', 'read_diff'); } if (afterDiagnosis !== diff) { diff = afterDiagnosis;