Skip to content

Commit fd02b9b

Browse files
committed
fix(server): PR watches stop trusting an inbox that missed a known comment
1 parent cf6d7bb commit fd02b9b

2 files changed

Lines changed: 58 additions & 11 deletions

File tree

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ interface LastRead {
9999
readonly watches: ReadonlySet<string>;
100100
readonly status: string | null;
101101
readonly remarks: string | null;
102+
readonly foreignRemarkAt: number | null;
102103
readonly detail: PullRequestDetail;
103104
}
104105

@@ -186,6 +187,24 @@ function planRead(
186187
};
187188
}
188189

190+
const viewerAuthored = (detail: PullRequestDetail) =>
191+
detail.viewer !== undefined && detail.viewer.toLowerCase() === detail.author?.login.toLowerCase();
192+
193+
const newestForeignRemark = (
194+
detail: PullRequestDetail,
195+
fingerprint: ProviderChangeRequestWatchFingerprint | null,
196+
remarks: ReadonlyArray<PullRequestComment>,
197+
) => {
198+
if (fingerprint?.viewerSubscription !== "SUBSCRIBED" || !viewerAuthored(detail)) return null;
199+
const viewer = detail.viewer!.toLowerCase();
200+
const times = remarks.flatMap((remark) =>
201+
remark.author === null || remark.author.login.toLowerCase() === viewer
202+
? []
203+
: [Date.parse(remark.createdAt)],
204+
);
205+
return times.length === 0 ? null : Math.max(...times);
206+
};
207+
189208
const notificationsCanGate = (last: LastRead, now: number) =>
190209
now - last.checkedAt >= FINGERPRINT_REREAD_MS &&
191210
now - Math.min(last.at, last.detailAt) < NOTIFIED_REREAD_CEILING_MS;
@@ -202,8 +221,7 @@ const gatesReread = (
202221
last.remarks === fingerprint.remarks &&
203222
last.remarksComplete &&
204223
notificationsCanGate(last, now) &&
205-
last.detail.viewer !== undefined &&
206-
last.detail.viewer.toLowerCase() === last.detail.author?.login.toLowerCase();
224+
viewerAuthored(last.detail);
207225

208226
function watchesEqual(left: ThreadPullRequestWatch, right: ThreadPullRequestWatch): boolean {
209227
return (
@@ -606,6 +624,9 @@ export const make = Effect.gen(function* () {
606624
watches: new Set(group.targets.map(watchKey)),
607625
status: fingerprint?.status ?? null,
608626
remarks: fingerprint?.remarks ?? null,
627+
foreignRemarkAt: Option.isSome(activity)
628+
? newestForeignRemark(detail, fingerprint, remarks ?? activity.value.comments)
629+
: (last?.foreignRemarkAt ?? null),
609630
detail,
610631
});
611632
yield* eachTarget(group, (target) => {
@@ -633,6 +654,7 @@ export const make = Effect.gen(function* () {
633654
});
634655

635656
const notificationsUnreadableAt = new Map<string, number>();
657+
const notificationsLiveThrough = new Map<string, number>();
636658

637659
const repositoryOf = (group: WatchGroup) => {
638660
const { projectId, host, repository } = referenceOf(group);
@@ -644,7 +666,10 @@ export const make = Effect.gen(function* () {
644666
groups: ReadonlyArray<WatchGroup>,
645667
) {
646668
const now = yield* Clock.currentTimeMillis;
647-
const due = new Map<string, { readonly group: WatchGroup; readonly since: number }>();
669+
const due = new Map<
670+
string,
671+
{ readonly group: WatchGroup; readonly since: number; readonly evidence: number }
672+
>();
648673
for (const group of groups) {
649674
const last = lastReads.get(group.key);
650675
const repository = repositoryOf(group);
@@ -655,21 +680,40 @@ export const make = Effect.gen(function* () {
655680
) {
656681
continue;
657682
}
658-
const since = Math.min(due.get(repository)?.since ?? last.at, last.at);
659-
due.set(repository, { group: due.get(repository)?.group ?? group, since });
683+
const prior = due.get(repository);
684+
due.set(repository, {
685+
group: prior?.group ?? group,
686+
since: Math.min(prior?.since ?? last.at, last.at),
687+
evidence: Math.max(prior?.evidence ?? -Infinity, last.foreignRemarkAt ?? -Infinity),
688+
});
660689
}
661690
const checks = new Map<
662691
string,
663692
Effect.Effect<ProviderChangeRequestNotifications | null | undefined>
664693
>();
665-
for (const [repository, { group, since }] of due) {
694+
for (const [repository, { group, since, evidence }] of due) {
695+
// With "On GitHub" notifications off the inbox never moves, so its silence counts only
696+
// once it has shown a remark someone else made on the viewer's own pull request.
697+
const unproven = evidence > (notificationsLiveThrough.get(repository) ?? -Infinity);
666698
const check = pullRequests
667-
.notifications({ ...referenceOf(group), since: since - NOTIFICATION_LAG_MS })
699+
.notifications({
700+
...referenceOf(group),
701+
since: (unproven ? Math.min(since, evidence) : since) - NOTIFICATION_LAG_MS,
702+
})
668703
.pipe(
704+
Effect.map((notifications) =>
705+
unproven && notifications?.threads.length === 0 && !notifications.truncated
706+
? null
707+
: notifications,
708+
),
669709
Effect.tap((notifications) =>
670710
Effect.sync(() => {
671-
if (notifications === null) notificationsUnreadableAt.set(repository, now);
672-
else notificationsUnreadableAt.delete(repository);
711+
if (notifications === null) {
712+
notificationsUnreadableAt.set(repository, now);
713+
return;
714+
}
715+
notificationsUnreadableAt.delete(repository);
716+
if (unproven) notificationsLiveThrough.set(repository, evidence);
673717
}),
674718
),
675719
Effect.catchCause((cause) =>

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8930,13 +8930,13 @@ it.layer(layerQueueResumeDisabledInstanceTest)(
89308930

89318931
const watchCostScenarios = [
89328932
{ name: "quiet PR", saves: 48 },
8933-
{ name: "review comment edited", reviewCommentEdit: 40, saves: 32 },
8933+
{ name: "review comment edited", reviewCommentEdit: 40, saves: 31 },
89348934
{
89358935
name: "review comment edit missing from the inbox",
89368936
reviewCommentEdit: 40,
89378937
inboxMissesEdit: true,
89388938
ceilingWake: 120,
8939-
saves: 48,
8939+
saves: 47,
89408940
},
89418941
{ name: "noisy inbox", inboxEveryMinute: true, saves: 45 },
89428942
{
@@ -8948,6 +8948,7 @@ const watchCostScenarios = [
89488948
},
89498949
{ name: "notifications readable again", notificationsReadableFrom: 60, saves: 31 },
89508950
{ name: "unsubscribed author", viewerSubscription: "UNSUBSCRIBED", saves: 0 },
8951+
{ name: "On GitHub notifications off", reviewCommentEdit: 40, inboxOff: true, saves: 0 },
89518952
];
89528953

89538954
it.layer(Layer.fresh(layerTest))("pull request watch request cost", (it) => {
@@ -9007,6 +9008,8 @@ it.layer(Layer.fresh(layerTest))("pull request watch request cost", (it) => {
90079008
const bump = (thread: number, time: number) => {
90089009
if (time <= now) times.set(thread, Math.max(times.get(thread) ?? 0, time));
90099010
};
9011+
if (scenario.inboxOff) return times;
9012+
if (editAt !== null) bump(number, at(-60) + 3_000);
90109013
if (editAt !== null && !scenario.inboxMissesEdit) bump(number, editAt + 3_000);
90119014
if (scenario.inboxEveryMinute) {
90129015
for (let minute = 0; at(minute) <= now; minute += 1) bump(900, at(minute) + 3_000);

0 commit comments

Comments
 (0)