Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions apps/api/src/services/reconcile-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ export type ExecuteOutcome =
| { status: "skipped"; reason: string }
| { status: "error"; reason: string; error: unknown };

/**
* Patch PR status fields in the task row after an edge-triggered action.
* This prevents duplicate edge detections on subsequent reconcile passes.
*/
async function patchPrStatusFields(taskId: string, snapshot: WorldSnapshot): Promise<void> {
if (snapshot.run.kind !== "repo" || !snapshot.pr) return;
await db
.update(tasks)
.set({
prChecksStatus: snapshot.pr.checksStatus,
prReviewStatus: snapshot.pr.reviewStatus,
prState: snapshot.pr.state,
})
.where(eq(tasks.id, taskId));
}

/**
* Apply a reconcile action. All DB mutations are CAS-gated on
* updated_at == snapshot.run.status.updatedAt so a decision made from
Expand Down Expand Up @@ -142,6 +158,13 @@ async function applyRepoTransition(
try {
await taskService.transitionTask(id, action.to, action.trigger, action.reason);
await scheduleBackoffReconcile(snapshot.run.ref, patch.reconcileBackoffUntil);

// Patch PR status fields if this was a PR-related edge trigger, to prevent
// duplicate edge detections on subsequent reconcile passes.
if (action.trigger?.match(/ci_failing|review_changes|conflicts/)) {
await patchPrStatusFields(id, snapshot);
}

return { status: "applied", reason: `transition:${action.to}` };
} catch (err) {
// StateRaceError means another worker won; treat as stale.
Expand Down Expand Up @@ -375,6 +398,8 @@ async function applyResumeAgent(
},
{ jobId: `${taskId}-${jobSuffix}-${Date.now()}` },
);

await patchPrStatusFields(taskId, snapshot);
return { status: "applied", reason: `resume:${action.resumeReason}` };
}

Expand All @@ -386,6 +411,7 @@ async function applyLaunchReview(snapshot: WorldSnapshot): Promise<ExecuteOutcom
const { launchReview } = await import("./review-service.js");
try {
await launchReview(taskId);
await patchPrStatusFields(taskId, snapshot);
return { status: "applied", reason: "review_launched" };
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
Expand Down
17 changes: 3 additions & 14 deletions apps/api/src/workers/pr-watcher-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ export function startPrWatcherWorker() {
const prData = await platform.getPullRequest(ri, prNumber).catch(() => null);
if (!prData) continue;

const checkRuns = await platform.getCIChecks(ri, prData.headSha).catch(() => []);
const reviewsData = await platform.getReviews(ri, prNumber).catch(() => []);
const checksStatus = determineCheckStatus(checkRuns);
const reviewResult = determineReviewStatus(reviewsData);
const reviewStatus = reviewResult.status;
let reviewComments = reviewResult.comments;
Expand All @@ -136,20 +134,11 @@ export function startPrWatcherWorker() {
} catch {}
}

// Conflicts override the raw checks status — once we've recorded
// conflicts, keep that label until mergeable flips back.
const effectiveChecksStatus =
task.prChecksStatus === "conflicts" && prData.mergeable === false
? "conflicts"
: checksStatus;

// Write all PR fields in one update. The reconciler reads these
// from the snapshot to decide the next action.
// Only update non-edge-triggering fields. The reconciler handles
// prChecksStatus, prReviewStatus, prState updates after detecting
// edge transitions — updating them here would defeat edge detection.
const updates: Record<string, unknown> = {
prNumber,
prState: prData.merged ? "merged" : prData.state,
prChecksStatus: effectiveChecksStatus,
prReviewStatus: reviewStatus,
updatedAt: new Date(),
};
if (reviewComments) {
Expand Down
Loading