Skip to content

Commit b22b040

Browse files
committed
Harden repair execution for rebase conflicts
1 parent c88270b commit b22b040

4 files changed

Lines changed: 75 additions & 6 deletions

File tree

‎src/repair/execute-fix-artifact.ts‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,18 +666,26 @@ try {
666666
});
667667
throw error;
668668
}
669+
const unresolvedRebaseConflicts = needsHumanRebaseConflictReason(error);
669670
outcome = {
670-
action: "execute_fix",
671+
action: unresolvedRebaseConflicts ? "needs_human" : "execute_fix",
671672
status: "blocked",
672673
repair_strategy: fixArtifact.repair_strategy,
673674
reason: error.message,
674-
...(isRetryableCodexFailure(error) ? { requeue_required: true } : {}),
675+
...(unresolvedRebaseConflicts
676+
? { needs_human: [unresolvedRebaseConflicts] }
677+
: isRetryableCodexFailure(error)
678+
? { requeue_required: true }
679+
: {}),
675680
};
676681
}
677682
}
678683

679-
report.status = outcome.status;
684+
report.status = outcome.action === "needs_human" ? "needs_human" : outcome.status;
680685
if (outcome.reason && !report.reason) report.reason = outcome.reason;
686+
if (Array.isArray(outcome.needs_human) && outcome.needs_human.length > 0) {
687+
report.needs_human = uniqueStrings([...(report.needs_human ?? []), ...outcome.needs_human]);
688+
}
681689
report.actions.push(outcome);
682690
writeReport(report, resultPath);
683691
if (outcome.requeue_required === true) process.exitCode = 1;
@@ -689,6 +697,11 @@ updateAutomergeProgressStatus({
689697
headSha: outcome.commit ?? null,
690698
});
691699

700+
function needsHumanRebaseConflictReason(error: JsonValue) {
701+
const message = String(error?.message ?? error ?? "");
702+
return /rebase conflicts remain unresolved:/i.test(message) ? message : null;
703+
}
704+
692705
function isRetryableCodexFailure(...values: JsonValue[]) {
693706
const messages = values.flat().map(String);
694707
const message = messages.join("\n");
@@ -1982,7 +1995,10 @@ function editValidatePrepareMerge({
19821995
repositoryContext,
19831996
reconcileWithBase,
19841997
sourceHead,
1985-
rebaseResult,
1998+
rebaseResult:
1999+
rebaseResult?.status === "conflicts"
2000+
? { ...rebaseResult, unmerged_paths: unmergedPaths(targetDir) }
2001+
: rebaseResult,
19862002
maxEditAttempts,
19872003
validationCommands: validationPreflight.resolved_commands ?? [],
19882004
isAutomergeRepair: isAutomergeRepairJob(),
@@ -2090,6 +2106,30 @@ function editValidatePrepareMerge({
20902106
headSha: currentHead(targetDir),
20912107
});
20922108

2109+
if (rebaseResult?.status === "conflicts") {
2110+
const remainingConflicts = unmergedPaths(targetDir);
2111+
if (remainingConflicts.length > 0) {
2112+
previousSummary =
2113+
readTextIfExists(summaryPath).trim() ||
2114+
`Rebase conflicts remain unresolved after edit attempt ${attempt}: ${remainingConflicts.join(", ")}`;
2115+
logProgress("rebase conflicts remain after Codex edit pass", {
2116+
mode,
2117+
attempt,
2118+
max_attempts: maxEditAttempts,
2119+
unresolved: remainingConflicts,
2120+
});
2121+
updateAutomergeProgressStatus({
2122+
id: `codex-edit-${mode}-${attempt}`,
2123+
label: `Codex edit ${attempt}`,
2124+
status: attempt < maxEditAttempts ? "retrying" : "blocked",
2125+
details: `unresolved rebase conflicts: ${remainingConflicts.join(", ")}`,
2126+
headSha: currentHead(targetDir),
2127+
});
2128+
if (attempt < maxEditAttempts) continue;
2129+
throw new Error(`rebase conflicts remain unresolved: ${remainingConflicts.join(", ")}`);
2130+
}
2131+
}
2132+
20932133
const hasWorkingTreeChanges = Boolean(
20942134
run("git", ["status", "--porcelain"], { cwd: targetDir }).trim(),
20952135
);

‎src/repair/fix-prompt-builder.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ test("fix prompt includes rebase and previous no-diff recovery details", () => {
127127
base_ref: "origin/main",
128128
base_sha: "def456",
129129
detail: "CONFLICT (content): CHANGELOG.md",
130+
unmerged_paths: ["src/index.ts", "CHANGELOG.md"],
130131
},
131132
maxEditAttempts: 5,
132133
});
@@ -136,7 +137,9 @@ test("fix prompt includes rebase and previous no-diff recovery details", () => {
136137
assert.match(prompt, /Existing repair branch detected/);
137138
assert.match(prompt, /Source head before edit: abc123/);
138139
assert.match(prompt, /Deterministic pre-edit rebase: conflicts onto origin\/main \(def456\)/);
139-
assert.match(prompt, /Resolve the active rebase conflicts/);
140+
assert.match(prompt, /Conflict-first mode/);
141+
assert.match(prompt, /resolve only the active rebase conflicts/);
142+
assert.match(prompt, /Unmerged conflict paths: src\/index\.ts, CHANGELOG\.md/);
140143
assert.match(prompt, /Rebase output: CONFLICT \(content\): CHANGELOG\.md/);
141144
assert.match(prompt, /Previous attempt produced no target repo diff/);
142145
assert.match(prompt, /Previous no-diff summary: Analyzed without editing files/);

‎src/repair/fix-prompt-builder.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,23 @@ function renderRebaseResult(rebaseResult: LooseRecord) {
276276
const baseRef = String(rebaseResult.base_ref ?? "origin/main");
277277
const baseSha = String(rebaseResult.base_sha ?? "unknown");
278278
const detail = compactText(String(rebaseResult.detail ?? "").trim(), 800);
279+
const unmergedPaths = Array.isArray(rebaseResult.unmerged_paths)
280+
? rebaseResult.unmerged_paths.map((entry: unknown) => String(entry)).filter(Boolean)
281+
: [];
279282
return [
280283
`Deterministic pre-edit rebase: ${status} onto ${baseRef} (${baseSha}).`,
281284
status === "conflicts"
282-
? "Resolve the active rebase conflicts, continue or finish the rebase, and leave the checkout in a normal non-rebasing state before returning."
285+
? [
286+
"Conflict-first mode:",
287+
"- resolve only the active rebase conflicts before doing any feature repair;",
288+
"- inspect `git status --short` and the conflicted files listed below;",
289+
"- remove all conflict markers and stage the resolved files;",
290+
"- run `git rebase --continue` until the checkout is no longer rebasing;",
291+
"- do not broaden the patch while conflicts remain.",
292+
].join("\n")
293+
: "",
294+
status === "conflicts" && unmergedPaths.length > 0
295+
? `Unmerged conflict paths: ${unmergedPaths.join(", ")}`
283296
: "",
284297
detail ? `Rebase output: ${detail}` : "",
285298
]

‎test/repair/execute-fix-artifact-source.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,16 @@ test("issue implementation rechecks opt-out labels immediately before branch pus
148148
assert.match(source.slice(helperStart, helperEnd), /repairPauseLabel\(issue\.labels\)/);
149149
assert.match(source.slice(helperStart, helperEnd), /refusing to push or open a PR/);
150150
});
151+
152+
153+
test("unresolved rebase conflicts are terminal needs_human repair reports", () => {
154+
const source = fs.readFileSync(
155+
path.join(process.cwd(), "src/repair/execute-fix-artifact.ts"),
156+
"utf8",
157+
);
158+
assert.match(source, /function needsHumanRebaseConflictReason/);
159+
assert.match(source, /rebase conflicts remain unresolved:/i);
160+
assert.match(source, /action: unresolvedRebaseConflicts \? "needs_human" : "execute_fix"/);
161+
assert.match(source, /report.status = outcome.action === "needs_human" \? "needs_human" : outcome.status/);
162+
assert.match(source, /report.needs_human = uniqueStrings/);
163+
});

0 commit comments

Comments
 (0)