Skip to content

Commit f39dd26

Browse files
committed
fix(repair): resume completed automerge verdicts
1 parent 05056e4 commit f39dd26

7 files changed

Lines changed: 240 additions & 13 deletions

File tree

‎src/repair/comment-router-core.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,20 @@ export function latestRepairLoopResumeTime(entries: JsonValue, command: LooseRec
749749
if (!entry || typeof entry !== "object") continue;
750750
if (entry.repo !== command.repo) continue;
751751
if (Number(entry.issue_number) !== Number(command.issue_number)) continue;
752-
if (!["autofix", "automerge"].includes(String(entry.intent ?? ""))) continue;
752+
const intent = String(entry.intent ?? "");
753+
if (!["autofix", "automerge", "maintainer_approve_automerge"].includes(intent)) continue;
753754
if (!["executed", "waiting"].includes(String(entry.status ?? ""))) continue;
755+
if (intent === "maintainer_approve_automerge") {
756+
const approvedHead = String(entry.expected_head_sha ?? entry.target?.head_sha ?? "")
757+
.trim()
758+
.toLowerCase();
759+
const reviewedHead = String(command.expected_head_sha ?? command.target?.head_sha ?? "")
760+
.trim()
761+
.toLowerCase();
762+
// An approval is stronger than a mode opt-in, so preserve it only for the
763+
// exact reviewed head it authorized. A later contributor push must reopen the gate.
764+
if (!approvedHead || approvedHead !== reviewedHead) continue;
765+
}
754766
latest = Math.max(latest, Date.parse(String(entry.comment_updated_at ?? "")) || 0);
755767
}
756768
return latest;
@@ -1979,20 +1991,23 @@ export function trustedExactHeadReviewCompletionSince({
19791991
headSha: string;
19801992
trustedAuthors?: ReadonlySet<string>;
19811993
sinceMs: number;
1982-
}): { reviewedAt: string | null; publishedAt: string | null } | null {
1994+
}): { commentId: number; reviewedAt: string | null; publishedAt: string | null } | null {
19831995
const normalizedHead = String(headSha ?? "")
19841996
.trim()
19851997
.toLowerCase();
19861998
if (!normalizedHead || !Number.isFinite(sinceMs)) return null;
19871999

19882000
let newest: {
2001+
commentId: number;
19892002
observedAtMs: number;
19902003
reviewedAt: string | null;
19912004
publishedAt: string | null;
19922005
} | null = null;
19932006
for (const comment of comments) {
19942007
const completed = parseTrustedAutomation(comment, { trustedAuthors });
19952008
if (!completed) continue;
2009+
const commentId = Number(comment?.id);
2010+
if (!Number.isInteger(commentId) || commentId <= 0) continue;
19962011
if (
19972012
String(completed.expected_head_sha ?? "")
19982013
.trim()
@@ -2018,10 +2033,16 @@ export function trustedExactHeadReviewCompletionSince({
20182033
);
20192034
if (observedAtMs < sinceMs || observedAtMs <= 0) continue;
20202035
if (!newest || observedAtMs > newest.observedAtMs) {
2021-
newest = { observedAtMs, reviewedAt, publishedAt };
2036+
newest = { commentId, observedAtMs, reviewedAt, publishedAt };
20222037
}
20232038
}
2024-
return newest && { reviewedAt: newest.reviewedAt, publishedAt: newest.publishedAt };
2039+
return (
2040+
newest && {
2041+
commentId: newest.commentId,
2042+
reviewedAt: newest.reviewedAt,
2043+
publishedAt: newest.publishedAt,
2044+
}
2045+
);
20252046
}
20262047

20272048
export function parseRoutedCommentCommand(

‎src/repair/comment-router.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,10 @@ function executeCommand(command: LooseRecord) {
20932093
);
20942094
const dispatchDecision = reviewDispatchDecisionForCommand(command);
20952095
if (dispatchDecision.action !== "dispatch") {
2096+
const verdictRouter =
2097+
dispatchDecision.action === "reuse_completed_review"
2098+
? dispatchCompletedReviewVerdict(command, dispatchDecision)
2099+
: null;
20962100
const dispatchStatus = markCoordinatedReviewDispatchActions({
20972101
command,
20982102
job,
@@ -2106,6 +2110,7 @@ function executeCommand(command: LooseRecord) {
21062110
status: dispatchStatus,
21072111
coordination_action: dispatchDecision.action,
21082112
reason: dispatchDecision.reason,
2113+
...(verdictRouter ? { verdict_router: verdictRouter } : {}),
21092114
},
21102115
};
21112116
}
@@ -2991,6 +2996,7 @@ function reviewDispatchDecisionForCommand(
29912996
headAfter: headBefore,
29922997
activeLeaseExpiresAt: null,
29932998
completedReviewAt: null,
2999+
completedReviewCommentId: null,
29943000
});
29953001
}
29963002
const comments = ghPaged<JsonValue>(
@@ -3026,6 +3032,7 @@ function reviewDispatchDecisionForCommand(
30263032
completedReview?.publishedAt ??
30273033
completedReview?.reviewedAt ??
30283034
(completedReview ? "recently" : null),
3035+
completedReviewCommentId: completedReview?.commentId ?? null,
30293036
});
30303037
} catch (error) {
30313038
return {
@@ -3036,6 +3043,7 @@ function reviewDispatchDecisionForCommand(
30363043
}
30373044

30383045
function reviewDispatchActionStatus(decision: ReviewDispatchCoordinationDecision) {
3046+
if (decision.action === "reuse_completed_review") return "executed";
30393047
return ["wait_for_active_review", "retry"].includes(decision.action) ? "waiting" : "skipped";
30403048
}
30413049

@@ -3333,6 +3341,53 @@ function dispatchClawSweeperReview(command: LooseRecord): LooseRecord {
33333341
};
33343342
}
33353343

3344+
function dispatchCompletedReviewVerdict(
3345+
command: LooseRecord,
3346+
decision: Extract<ReviewDispatchCoordinationDecision, { action: "reuse_completed_review" }>,
3347+
) {
3348+
const attemptId = `completed-review-${command.comment_id ?? "sweep"}-${decision.commentId}`;
3349+
const payload = JSON.stringify({
3350+
event_type: "clawsweeper_comment",
3351+
client_payload: {
3352+
target_repo: command.repo,
3353+
...(command.target_branch ? { target_branch: String(command.target_branch) } : {}),
3354+
item_number: String(command.issue_number),
3355+
comment_id: String(decision.commentId),
3356+
max_comments: "1",
3357+
force_reprocess: "true",
3358+
attempt_id: attemptId,
3359+
source_event: "comment_router",
3360+
source_action: "reuse_completed_review",
3361+
},
3362+
});
3363+
const result = runGitHubSpawnMutation(
3364+
command,
3365+
"review_verdict_dispatch",
3366+
{
3367+
repository: reviewRepo,
3368+
workflow: "repair-comment-router.yml",
3369+
event: "repository_dispatch",
3370+
commentId: decision.commentId,
3371+
attemptId,
3372+
},
3373+
["api", `repos/${reviewRepo}/dispatches`, "--method", "POST", "--input", "-"],
3374+
{ env: dispatchTokenEnv(), input: payload },
3375+
);
3376+
if (result.status !== 0) {
3377+
throw new Error(
3378+
`failed to requeue completed ClawSweeper verdict for #${command.issue_number}: ${stripAnsi(result.stderr || result.stdout).trim()}`,
3379+
);
3380+
}
3381+
return {
3382+
workflow: "repair-comment-router.yml",
3383+
event: "repository_dispatch",
3384+
repo: reviewRepo,
3385+
item_number: command.issue_number,
3386+
comment_id: decision.commentId,
3387+
attempt_id: attemptId,
3388+
};
3389+
}
3390+
33363391
function dispatchClawSweeperAssist(command: LooseRecord): LooseRecord {
33373392
let dispatchKey = dispatchReceiptKey(command);
33383393
const workflowName = "assist.yml";

‎src/repair/review-dispatch-coordination.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export type ReviewDispatchCoordinationDecision =
22
| { action: "dispatch" }
33
| { action: "wait_for_active_review"; reason: string }
4-
| { action: "reuse_completed_review"; reason: string }
4+
| { action: "reuse_completed_review"; commentId: number; reason: string }
55
| { action: "retry"; reason: string }
66
| { action: "stop"; reason: string };
77

@@ -12,6 +12,7 @@ export type ReviewDispatchCoordinationInput = {
1212
headAfter: string;
1313
activeLeaseExpiresAt: string | null;
1414
completedReviewAt: string | null;
15+
completedReviewCommentId: number | null;
1516
};
1617

1718
export function decideReviewDispatchCoordination({
@@ -21,6 +22,7 @@ export function decideReviewDispatchCoordination({
2122
headAfter,
2223
activeLeaseExpiresAt,
2324
completedReviewAt,
25+
completedReviewCommentId,
2426
}: ReviewDispatchCoordinationInput): ReviewDispatchCoordinationDecision {
2527
if (!isOpen(stateBefore) || !isOpen(stateAfter)) {
2628
return { action: "stop", reason: "target is no longer an open PR" };
@@ -39,9 +41,10 @@ export function decideReviewDispatchCoordination({
3941
reason: `same-head ClawSweeper review is active until ${activeLeaseExpiresAt}`,
4042
};
4143
}
42-
if (completedReviewAt) {
44+
if (completedReviewAt && completedReviewCommentId) {
4345
return {
4446
action: "reuse_completed_review",
47+
commentId: completedReviewCommentId,
4548
reason: `same-head ClawSweeper review completed at ${completedReviewAt}; its result will be reused`,
4649
};
4750
}

‎test/e2e/automerge/fake-gh.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if (args[0] === "pr" && args[1] === "view") {
7676
labels: state.pr.labels.map((name) => ({ name })),
7777
mergeable: "MERGEABLE",
7878
mergeCommit: state.pr.mergeCommitSha ? { oid: state.pr.mergeCommitSha } : null,
79-
mergeStateStatus: "CLEAN",
79+
mergeStateStatus: state.pr.mergeStateStatus,
8080
mergedAt: state.pr.mergedAt,
8181
reviewDecision: "APPROVED",
8282
state: state.pr.mergedAt ? "MERGED" : state.pr.state.toUpperCase(),
@@ -351,6 +351,9 @@ function mergePullRequest(expectedHead) {
351351
const head = currentHead();
352352
if (expectedHead && expectedHead !== head)
353353
fail(`head branch was modified: expected ${expectedHead}, found ${head}`);
354+
if (state.pr.mergeStateStatus === "BEHIND") {
355+
fail("pull request is not mergeable: the head branch is not up to date with the base branch");
356+
}
354357
if (state.pr.mergedAt) return;
355358
const work = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-e2e-merge-"));
356359
try {
@@ -388,6 +391,7 @@ function addComment(body) {
388391

389392
function readInput() {
390393
const inputPath = optionValue("--input");
394+
if (inputPath === "-") return JSON.parse(fs.readFileSync(0, "utf8"));
391395
return inputPath ? JSON.parse(fs.readFileSync(inputPath, "utf8")) : {};
392396
}
393397

0 commit comments

Comments
 (0)