From 54fc6ee49407b1c198eddf788e9584aa9802ab22 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Fri, 14 Aug 2026 12:16:29 +0800 Subject: [PATCH] fix: repair conflict diff check failures --- src/review/conflictResolver.ts | 62 +++++++++++++++++++++++++++++++--- test/conflict-resolver.test.ts | 4 +++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/review/conflictResolver.ts b/src/review/conflictResolver.ts index 871afc9..ec3aa71 100644 --- a/src/review/conflictResolver.ts +++ b/src/review/conflictResolver.ts @@ -164,8 +164,8 @@ export async function resolvePullRequestConflicts( await writeKnowledgeScratch(snapshot, knowledge); const beforeAgent = await inventorySnapshot(snapshot); await runGooseAgent(buildConflictPrompt(params, conflictFiles), snapshot); - const afterAgent = await inventorySnapshot(snapshot); - const agentChanges = diffSnapshotInventories(beforeAgent, afterAgent); + let afterAgent = await inventorySnapshot(snapshot); + let agentChanges = diffSnapshotInventories(beforeAgent, afterAgent); if (agentChanges.length === 0) { throw new Error("goose did not modify any files while resolving conflicts."); } @@ -178,7 +178,44 @@ export async function resolvePullRequestConflicts( if (remaining.length > 0) { throw new Error(`goose left unresolved conflicts in: ${remaining.join(", ")}`); } - await runCommand("git", ["diff", "--check", "--cached"], worktree, gitEnv); + let diffCheck = await runCommandAllowFailure( + "git", + ["diff", "--check", "--cached"], + worktree, + gitEnv + ); + if (diffCheck.code !== 0) { + const previousAgent = afterAgent; + await runGooseAgent(buildDiffCheckRepairPrompt(commandFailureOutput(diffCheck)), snapshot); + afterAgent = await inventorySnapshot(snapshot); + const correctionChanges = diffSnapshotInventories(previousAgent, afterAgent); + if (correctionChanges.length === 0) { + throw new Error( + `git diff --check failed and goose did not correct the reported files: ${commandFailureOutput(diffCheck)}` + ); + } + await applySnapshotChanges(snapshot, worktree, correctionChanges, afterAgent); + await assertConflictMarkersRemoved(worktree, conflictFiles); + await runCommand("git", ["add", "-A"], worktree, gitEnv); + const remainingAfterCorrection = splitNullSeparated( + await runCommand("git", ["diff", "--name-only", "--diff-filter=U", "-z"], worktree, gitEnv) + ); + if (remainingAfterCorrection.length > 0) { + throw new Error( + `goose reintroduced unresolved conflicts while correcting diff issues: ${remainingAfterCorrection.join(", ")}` + ); + } + diffCheck = await runCommandAllowFailure( + "git", + ["diff", "--check", "--cached"], + worktree, + gitEnv + ); + if (diffCheck.code !== 0) { + throw new Error(`git diff --check failed after goose correction: ${commandFailureOutput(diffCheck)}`); + } + agentChanges = diffSnapshotInventories(beforeAgent, afterAgent); + } const finalDiff = await runCommand( "git", @@ -333,6 +370,9 @@ export function describeConflictResolutionFailure(error: unknown): string { if (message.includes("final goose confirmation rejected") || message.includes("validation command")) { return "goose produced a candidate resolution, but the configured validation or final safety confirmation rejected it. No commit was pushed."; } + if (message.includes("git diff --check failed")) { + return "The candidate resolution still contained Git whitespace or conflict-marker errors after one automatic correction pass. No commit was pushed."; + } return "goose could not safely complete and validate the conflict resolution. The Actions log contains the exact failure stage, and no commit was pushed."; } @@ -379,6 +419,16 @@ function buildConflictPrompt( ].join("\n"); } +function buildDiffCheckRepairPrompt(checkOutput: string): string { + return [ + "Correct the current conflict-resolution workspace so the host Git diff check passes.", + "Change only the files and lines needed to address the reported whitespace or conflict-marker diagnostics. Preserve the resolved behavior and do not add features, refactor unrelated code, or modify repository knowledge.", + "Inspect the affected files directly, apply the corrections, and return a concise summary. Do not commit or push.", + "Host git diff --check diagnostics:", + checkOutput.slice(0, 12_000) + ].join("\n"); +} + async function confirmFinalResolution(input: { pullNumber: number; baseBranch: string; @@ -576,11 +626,15 @@ async function runCommand( ): Promise { const result = await runCommandAllowFailure(command, args, cwd, extraEnv); if (result.code !== 0) { - throw new Error(`${command} exited with code ${result.code}: ${result.stderr.trim()}`); + throw new Error(`${command} exited with code ${result.code}: ${commandFailureOutput(result)}`); } return result.stdout; } +function commandFailureOutput(result: { stdout: string; stderr: string }): string { + return [result.stderr.trim(), result.stdout.trim()].filter(Boolean).join("\n").slice(0, 20_000) || "no output"; +} + async function runCommandAllowFailure( command: string, args: string[], diff --git a/test/conflict-resolver.test.ts b/test/conflict-resolver.test.ts index 1ed806c..0caed91 100644 --- a/test/conflict-resolver.test.ts +++ b/test/conflict-resolver.test.ts @@ -46,6 +46,10 @@ test("conflict failures are actionable without exposing raw command output", () describeConflictResolutionFailure(new Error("Committer identity unknown")), /no bot committer identity/ ); + assert.match( + describeConflictResolutionFailure(new Error("git diff --check failed after goose correction")), + /automatic correction pass/ + ); assert.match( describeConflictResolutionFailure(new Error("rejected: stale info")), /Run \/conflict again/