Skip to content

Commit 4479100

Browse files
fix(server): respect pull request watch generations
1 parent ea16ad3 commit 4479100

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,21 @@ function nextQueuedRun(
617617
return queuedRunsInDeliveryOrder(projection)[0];
618618
}
619619

620+
function pullRequestSnapshotEndsWatch(
621+
snapshot: ThreadPullRequestLink["snapshot"],
622+
watchStartedAt: ThreadPullRequestWatch["startedAt"],
623+
): boolean {
624+
return (
625+
snapshot?.state === "merged" ||
626+
// A restarted watch can see a closed snapshot from before the PR reopened.
627+
(snapshot?.state === "closed" &&
628+
DateTime.isGreaterThanOrEqualTo(
629+
DateTime.makeUnsafe(snapshot.syncedAt),
630+
DateTime.makeUnsafe(watchStartedAt),
631+
))
632+
);
633+
}
634+
620635
function isStalePullRequestWatchWake(
621636
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "messages">,
622637
run: OrchestrationV2Run,
@@ -633,14 +648,13 @@ function isStalePullRequestWatchWake(
633648
if (stamp.watchEnded === true) {
634649
// The final notice outlives its watch, but a replacement watch supersedes it.
635650
const latestWatch = link.watch ?? link.endedWatch;
636-
return latestWatch !== undefined && latestWatch.startedAt !== stamp.watchStartedAt;
651+
return latestWatch?.startedAt !== stamp.watchStartedAt;
637652
}
638653
return (
639654
link.watch === undefined ||
640655
link.watch.startedAt !== stamp.watchStartedAt ||
641656
link.watch.headSha !== stamp.headSha ||
642-
link.snapshot?.state === "merged" ||
643-
link.snapshot?.state === "closed"
657+
pullRequestSnapshotEndsWatch(link.snapshot, link.watch.startedAt)
644658
);
645659
}
646660

@@ -2441,7 +2455,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
24412455
if (
24422456
command.wake.notification.source.kind === "monitor" &&
24432457
command.watch !== null &&
2444-
(link.snapshot?.state === "merged" || link.snapshot?.state === "closed")
2458+
pullRequestSnapshotEndsWatch(link.snapshot, command.startedAt)
24452459
) {
24462460
return;
24472461
}

0 commit comments

Comments
 (0)