Skip to content

Commit d59bdb1

Browse files
steipeteJhacarreiro
andcommitted
fix(repair): harden rebase conflict retries
Co-authored-by: João Carreiro <[email protected]>
1 parent 25a378a commit d59bdb1

9 files changed

Lines changed: 242 additions & 54 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ checkpoint, and status-only commits are intentionally omitted.
1313

1414
### Fixed
1515

16+
- Kept unresolved rebase conflicts inside the bounded Codex repair loop and reported exhausted conflicts as human-required with exact paths. Thanks @Jhacarreiro.
1617
- Restored the Codex spawn helper to spam workflow sparse checkouts so repair builds can start.
1718
- Removed unconditional ffmpeg provisioning from review startup so optional media proof cannot block exact-review leases; unavailable media tools remain per-item evidence failures.
1819
- Prevented contributor-branch repairs and changelog-free repair artifacts from adding release-owned changelog entries, keeping contributor credit and release-note context in PR bodies or commit history instead.

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

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ import {
114114
type TargetValidationOptions,
115115
} from "./target-validation.js";
116116
import { uniqueStrings } from "./validation-command-utils.js";
117+
import {
118+
rebaseConflictEditDecision,
119+
unresolvedRebaseConflictReason,
120+
} from "./rebase-conflict-policy.js";
117121
import { validateActivePrAreaCapacity } from "./execute-fix-area-capacity.js";
122+
import { issueImplementationTerminalOutcome } from "./issue-implementation-outcome.js";
118123
import {
119124
repairPauseLabel,
120125
validateAutonomousFixScope,
@@ -666,18 +671,26 @@ try {
666671
});
667672
throw error;
668673
}
674+
const unresolvedRebaseConflicts = unresolvedRebaseConflictReason(error);
669675
outcome = {
670-
action: "execute_fix",
676+
action: unresolvedRebaseConflicts ? "needs_human" : "execute_fix",
671677
status: "blocked",
672678
repair_strategy: fixArtifact.repair_strategy,
673679
reason: error.message,
674-
...(isRetryableCodexFailure(error) ? { requeue_required: true } : {}),
680+
...(unresolvedRebaseConflicts
681+
? { needs_human: [unresolvedRebaseConflicts] }
682+
: isRetryableCodexFailure(error)
683+
? { requeue_required: true }
684+
: {}),
675685
};
676686
}
677687
}
678688

679-
report.status = outcome.status;
689+
report.status = outcome.action === "needs_human" ? "needs_human" : outcome.status;
680690
if (outcome.reason && !report.reason) report.reason = outcome.reason;
691+
if (Array.isArray(outcome.needs_human) && outcome.needs_human.length > 0) {
692+
report.needs_human = uniqueStrings([...(report.needs_human ?? []), ...outcome.needs_human]);
693+
}
681694
report.actions.push(outcome);
682695
writeReport(report, resultPath);
683696
if (outcome.requeue_required === true) process.exitCode = 1;
@@ -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(),
@@ -2029,37 +2045,49 @@ function editValidatePrepareMerge({
20292045
stderrPath: path.join(workRoot, `${mode}-codex-${attempt}.stderr.log`),
20302046
},
20312047
);
2032-
if ((codexResult.error as JsonValue)?.code === "ETIMEDOUT") {
2033-
throw new Error(`Codex fix worker timed out after ${workerTimeoutMs}ms`);
2034-
}
2035-
if (codexResult.error) {
2036-
const errorDetail = codexFailureDetail(
2037-
codexResult,
2038-
codexResult.error.message || String(codexResult.error),
2039-
);
2040-
if (attempt < maxEditAttempts && isRetryableCodexErrorMessage(errorDetail)) {
2041-
previousSummary = compactText(errorDetail, 360);
2042-
const retryDelayMs = codexRetryDelayMs(errorDetail, attempt);
2043-
logProgress("retrying Codex edit pass after transient transport error", {
2044-
mode,
2048+
const timedOut = (codexResult.error as JsonValue)?.code === "ETIMEDOUT";
2049+
const errorDetail = timedOut
2050+
? `Codex fix worker timed out after ${workerTimeoutMs}ms`
2051+
: codexResult.error
2052+
? codexFailureDetail(codexResult, codexResult.error.message || String(codexResult.error))
2053+
: codexResult.status !== 0
2054+
? codexFailureDetail(codexResult, "Codex fix worker failed")
2055+
: "";
2056+
const remainingConflicts =
2057+
rebaseResult?.status === "conflicts" ? unmergedPaths(targetDir) : [];
2058+
// Worker failures keep their transport/terminal classification. Only a completed
2059+
// edit pass that leaves conflicts consumes the bounded conflict-repair budget.
2060+
const conflictDecision = errorDetail
2061+
? { action: "proceed" as const }
2062+
: rebaseConflictEditDecision({
2063+
rebaseStatus: rebaseResult?.status,
2064+
unmergedPaths: remainingConflicts,
20452065
attempt,
2046-
max_attempts: maxEditAttempts,
2047-
retry_delay_ms: retryDelayMs,
2048-
});
2049-
updateAutomergeProgressStatus({
2050-
id: `codex-edit-${mode}-${attempt}`,
2051-
label: `Codex edit ${attempt}`,
2052-
status: "retrying",
2053-
details: "transient Codex transport error",
2054-
headSha: currentHead(targetDir),
2066+
maxEditAttempts,
20552067
});
2056-
sleepMs(retryDelayMs);
2057-
continue;
2058-
}
2059-
throw new Error(codexFailureMessage("Codex fix worker failed", errorDetail));
2068+
if (conflictDecision.action !== "proceed") {
2069+
previousSummary =
2070+
readTextIfExists(summaryPath).trim() ||
2071+
compactText(errorDetail, 360) ||
2072+
`Rebase conflicts remain unresolved after edit attempt ${attempt}: ${remainingConflicts.join(", ")}`;
2073+
logProgress("rebase conflicts remain after Codex edit pass", {
2074+
mode,
2075+
attempt,
2076+
max_attempts: maxEditAttempts,
2077+
unresolved: remainingConflicts,
2078+
});
2079+
updateAutomergeProgressStatus({
2080+
id: `codex-edit-${mode}-${attempt}`,
2081+
label: `Codex edit ${attempt}`,
2082+
status: conflictDecision.action === "retry" ? "retrying" : "blocked",
2083+
details: `unresolved rebase conflicts: ${remainingConflicts.join(", ")}`,
2084+
headSha: currentHead(targetDir),
2085+
});
2086+
if (conflictDecision.action === "retry") continue;
2087+
throw new Error(conflictDecision.reason);
20602088
}
2061-
if (codexResult.status !== 0) {
2062-
const errorDetail = codexFailureDetail(codexResult, "Codex fix worker failed");
2089+
if (timedOut) throw new Error(errorDetail);
2090+
if (codexResult.error || codexResult.status !== 0) {
20632091
if (attempt < maxEditAttempts && isRetryableCodexErrorMessage(errorDetail)) {
20642092
previousSummary = compactText(errorDetail, 360);
20652093
const retryDelayMs = codexRetryDelayMs(errorDetail, attempt);
@@ -3458,25 +3486,6 @@ function issueImplementationPrAction(report: LooseRecord) {
34583486
});
34593487
}
34603488

3461-
function issueImplementationTerminalOutcome(report: LooseRecord) {
3462-
const actionOutcome = [...(report.actions ?? [])].reverse().find((action: JsonValue) => {
3463-
const status = String(action?.status ?? "").toLowerCase();
3464-
if (!["blocked", "skipped", "failed"].includes(status)) return false;
3465-
return ["execute_fix", "open_fix_pr", "repair_contributor_branch"].includes(
3466-
String(action?.action ?? ""),
3467-
);
3468-
});
3469-
if (actionOutcome) return actionOutcome;
3470-
3471-
const status = String(report.status ?? "").toLowerCase();
3472-
if (!["blocked", "skipped", "failed"].includes(status)) return null;
3473-
return {
3474-
action: "issue_implementation",
3475-
status,
3476-
reason: report.reason,
3477-
};
3478-
}
3479-
34803489
function issueImplementationTargetIssueNumber() {
34813490
for (const ref of [
34823491
...(job.frontmatter.canonical ?? []),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ test("fix prompt includes rebase and previous no-diff recovery details", () => {
144144
base_ref: "origin/main",
145145
base_sha: "def456",
146146
detail: "CONFLICT (content): CHANGELOG.md",
147+
unmerged_paths: ["src/index.ts", "CHANGELOG.md"],
147148
},
148149
maxEditAttempts: 5,
149150
});
@@ -153,7 +154,9 @@ test("fix prompt includes rebase and previous no-diff recovery details", () => {
153154
assert.match(prompt, /Existing repair branch detected/);
154155
assert.match(prompt, /Source head before edit: abc123/);
155156
assert.match(prompt, /Deterministic pre-edit rebase: conflicts onto origin\/main \(def456\)/);
156-
assert.match(prompt, /Resolve the active rebase conflicts/);
157+
assert.match(prompt, /Conflict-first mode/);
158+
assert.match(prompt, /resolve only the active rebase conflicts/);
159+
assert.match(prompt, /Unmerged conflict paths: src\/index\.ts, CHANGELOG\.md/);
157160
assert.match(prompt, /Rebase output: CONFLICT \(content\): CHANGELOG\.md/);
158161
assert.match(prompt, /Previous attempt produced no target repo diff/);
159162
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
@@ -284,10 +284,23 @@ function renderRebaseResult(rebaseResult: LooseRecord) {
284284
const baseRef = String(rebaseResult.base_ref ?? "origin/main");
285285
const baseSha = String(rebaseResult.base_sha ?? "unknown");
286286
const detail = compactText(String(rebaseResult.detail ?? "").trim(), 800);
287+
const unmergedPaths = Array.isArray(rebaseResult.unmerged_paths)
288+
? rebaseResult.unmerged_paths.map((entry: unknown) => String(entry)).filter(Boolean)
289+
: [];
287290
return [
288291
`Deterministic pre-edit rebase: ${status} onto ${baseRef} (${baseSha}).`,
289292
status === "conflicts"
290-
? "Resolve the active rebase conflicts, continue or finish the rebase, and leave the checkout in a normal non-rebasing state before returning."
293+
? [
294+
"Conflict-first mode:",
295+
"- resolve only the active rebase conflicts before doing any feature repair;",
296+
"- inspect `git status --short` and the conflicted files listed below;",
297+
"- remove all conflict markers and stage the resolved files;",
298+
"- run `git rebase --continue` until the checkout is no longer rebasing;",
299+
"- do not broaden the patch while conflicts remain.",
300+
].join("\n")
301+
: "",
302+
status === "conflicts" && unmergedPaths.length > 0
303+
? `Unmerged conflict paths: ${unmergedPaths.join(", ")}`
291304
: "",
292305
detail ? `Rebase output: ${detail}` : "",
293306
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { issueImplementationTerminalOutcome } from "./issue-implementation-outcome.js";
5+
6+
test("issue implementation treats needs-human repair actions as terminal", () => {
7+
const outcome = issueImplementationTerminalOutcome({
8+
status: "needs_human",
9+
actions: [
10+
{
11+
action: "needs_human",
12+
status: "blocked",
13+
reason: "rebase conflicts remain unresolved: src/index.ts",
14+
},
15+
],
16+
});
17+
18+
assert.deepEqual(outcome, {
19+
action: "needs_human",
20+
status: "blocked",
21+
reason: "rebase conflicts remain unresolved: src/index.ts",
22+
});
23+
});
24+
25+
test("issue implementation normalizes needs-human report fallback to blocked", () => {
26+
assert.deepEqual(
27+
issueImplementationTerminalOutcome({
28+
status: "needs_human",
29+
reason: "rebase produced additional conflicts: src/later.ts",
30+
}),
31+
{
32+
action: "issue_implementation",
33+
status: "blocked",
34+
reason: "rebase produced additional conflicts: src/later.ts",
35+
},
36+
);
37+
});
38+
39+
test("issue implementation ignores non-terminal reports", () => {
40+
assert.equal(issueImplementationTerminalOutcome({ status: "complete", actions: [] }), null);
41+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { JsonValue, LooseRecord } from "./json-types.js";
2+
3+
export function issueImplementationTerminalOutcome(report: LooseRecord) {
4+
const actionOutcome = [...(report.actions ?? [])].reverse().find((action: JsonValue) => {
5+
const status = String(action?.status ?? "").toLowerCase();
6+
if (!["blocked", "skipped", "failed"].includes(status)) return false;
7+
return ["execute_fix", "open_fix_pr", "repair_contributor_branch", "needs_human"].includes(
8+
String(action?.action ?? ""),
9+
);
10+
});
11+
if (actionOutcome) return actionOutcome;
12+
13+
const status = String(report.status ?? "").toLowerCase();
14+
if (!["blocked", "skipped", "failed", "needs_human"].includes(status)) return null;
15+
return {
16+
action: "issue_implementation",
17+
status: status === "needs_human" ? "blocked" : status,
18+
reason: report.reason,
19+
};
20+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import {
5+
rebaseConflictEditDecision,
6+
unresolvedRebaseConflictReason,
7+
} from "./rebase-conflict-policy.js";
8+
9+
test("unresolved rebase conflicts retry within the edit budget", () => {
10+
assert.deepEqual(
11+
rebaseConflictEditDecision({
12+
rebaseStatus: "conflicts",
13+
unmergedPaths: ["src/index.ts", "CHANGELOG.md"],
14+
attempt: 1,
15+
maxEditAttempts: 2,
16+
}),
17+
{
18+
action: "retry",
19+
reason: "rebase conflicts remain unresolved: src/index.ts, CHANGELOG.md",
20+
},
21+
);
22+
});
23+
24+
test("unresolved rebase conflicts require human help after the edit budget", () => {
25+
const decision = rebaseConflictEditDecision({
26+
rebaseStatus: "conflicts",
27+
unmergedPaths: ["src/index.ts"],
28+
attempt: 2,
29+
maxEditAttempts: 2,
30+
});
31+
32+
assert.deepEqual(decision, {
33+
action: "needs_human",
34+
reason: "rebase conflicts remain unresolved: src/index.ts",
35+
});
36+
assert.equal(unresolvedRebaseConflictReason(new Error(decision.reason)), decision.reason);
37+
});
38+
39+
test("resolved or non-conflicting rebases proceed", () => {
40+
assert.deepEqual(
41+
rebaseConflictEditDecision({
42+
rebaseStatus: "conflicts",
43+
unmergedPaths: [],
44+
attempt: 1,
45+
maxEditAttempts: 2,
46+
}),
47+
{ action: "proceed" },
48+
);
49+
assert.deepEqual(
50+
rebaseConflictEditDecision({
51+
rebaseStatus: "rebased",
52+
unmergedPaths: ["src/index.ts"],
53+
attempt: 2,
54+
maxEditAttempts: 2,
55+
}),
56+
{ action: "proceed" },
57+
);
58+
});
59+
60+
test("later conflicts from rebase continuation require human help", () => {
61+
const reason = "rebase produced additional conflicts: src/later.ts";
62+
assert.equal(unresolvedRebaseConflictReason(new Error(reason)), reason);
63+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { JsonValue } from "./json-types.js";
2+
3+
export type RebaseConflictEditDecision =
4+
| { action: "proceed" }
5+
| { action: "retry"; reason: string }
6+
| { action: "needs_human"; reason: string };
7+
8+
export function rebaseConflictEditDecision({
9+
rebaseStatus,
10+
unmergedPaths,
11+
attempt,
12+
maxEditAttempts,
13+
}: {
14+
rebaseStatus: JsonValue;
15+
unmergedPaths: string[];
16+
attempt: number;
17+
maxEditAttempts: number;
18+
}): RebaseConflictEditDecision {
19+
if (rebaseStatus !== "conflicts" || unmergedPaths.length === 0) {
20+
return { action: "proceed" };
21+
}
22+
23+
const reason = `rebase conflicts remain unresolved: ${unmergedPaths.join(", ")}`;
24+
if (attempt >= maxEditAttempts) return { action: "needs_human", reason };
25+
return { action: "retry", reason };
26+
}
27+
28+
export function unresolvedRebaseConflictReason(error: JsonValue): string | null {
29+
const message = String(error?.message ?? error ?? "");
30+
return /rebase (?:conflicts remain unresolved|produced additional conflicts):/i.test(message)
31+
? message
32+
: null;
33+
}

‎test/repair/git-repo-utils.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import path from "node:path";
55
import { spawnSync } from "node:child_process";
66
import test from "node:test";
77

8-
import { completeRebaseIfResolved, rebaseOntoBase } from "../../dist/repair/git-repo-utils.js";
8+
import {
9+
completeRebaseIfResolved,
10+
rebaseOntoBase,
11+
unmergedPaths,
12+
} from "../../dist/repair/git-repo-utils.js";
913

1014
test("rebaseOntoBase rebases a repair branch onto latest origin main", () => {
1115
const { work } = fixtureRepo();
@@ -46,6 +50,7 @@ test("rebaseOntoBase leaves conflicts for Codex repair instead of aborting", ()
4650
const result = rebaseOntoBase({ targetDir: work, baseBranch: "main" });
4751

4852
assert.equal(result.status, "conflicts");
53+
assert.deepEqual(unmergedPaths(work), ["shared.txt"]);
4954
assert.match(fs.readFileSync(path.join(work, "shared.txt"), "utf8"), /<<<<<<< HEAD/);
5055
});
5156

0 commit comments

Comments
 (0)