Skip to content

Commit c1daaef

Browse files
committed
fix(server): page a watched review thread again only when its count moves
The watcher swept every minute and re-read every page of every long review thread each time. Keep the pages past the first per watch and reuse them until the host's comment count for that thread changes.
1 parent ed9a7b2 commit c1daaef

2 files changed

Lines changed: 50 additions & 25 deletions

File tree

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
MessageId,
44
type OrchestrationV2Notification,
55
type PullRequestActivity,
6+
type PullRequestComment,
67
type PullRequestRef,
8+
type PullRequestThreadCommentsResult,
79
type ThreadPullRequestLink,
810
type ThreadPullRequestWatch,
911
} from "@t3tools/contracts";
@@ -83,6 +85,12 @@ export const make = Effect.gen(function* () {
8385

8486
// Passes in a row that failed, per watch. Kept in memory: a restart only delays the stop.
8587
const readFailures = new Map<string, number>();
88+
// Replies past each long thread's first page, per watch, so a pass pages a thread again only
89+
// when the host's count of it moves. Kept in memory: a restart pages each thread once more.
90+
const threadTails = new Map<
91+
string,
92+
Map<string, { readonly count: number; readonly comments: ReadonlyArray<PullRequestComment> }>
93+
>();
8694

8795
// Host-level identity, with the repository as linked, the way pull request sync reads it.
8896
const identityOf = (link: ThreadPullRequestLink) => ({
@@ -128,40 +136,51 @@ export const make = Effect.gen(function* () {
128136
}).pipe(Effect.catch(() => record(target, null)));
129137

130138
const readRemarks = Effect.fn("PullRequestWatchReactor.readRemarks")(
131-
function* (reference: PullRequestRef, activity: PullRequestActivity) {
139+
function* (target: WatchTarget, reference: PullRequestRef, activity: PullRequestActivity) {
132140
// Comment cursors cannot account for missing threads. Only finish a truncated read
133141
// when the host confirms that every thread was listed.
134142
if (activity.commentsTruncated && activity.reviewThreadsTruncated !== false) return null;
135143

136-
const comments = new Map(activity.comments.map((comment) => [comment.id, comment]));
144+
const key = failureKey(target);
145+
let tails = threadTails.get(key);
146+
if (tails === undefined) {
147+
tails = new Map();
148+
threadTails.set(key, tails);
149+
}
150+
const remarks = [...activity.comments];
137151
for (const thread of activity.reviewThreads) {
138-
const ids = new Set(thread.comments.map((comment) => comment.id));
139-
const cursors = new Set<string>();
140152
let cursor = thread.nextCommentsCursor ?? null;
141-
while (cursor !== null) {
142-
if (cursors.has(cursor)) return null;
143-
cursors.add(cursor);
144-
const page = yield* pullRequests.threadComments({
145-
...reference,
146-
threadId: thread.id,
147-
cursor,
148-
});
149-
for (const comment of page.comments) {
150-
ids.add(comment.id);
151-
comments.set(comment.id, {
152-
...comment,
153-
kind: "review-comment",
154-
path: thread.path,
155-
reviewState: null,
153+
if (cursor === null) continue;
154+
const count = thread.commentCount ?? 0;
155+
let tail = tails.get(thread.id);
156+
if (tail?.count !== count) {
157+
const comments = new Map<string, PullRequestComment>();
158+
const cursors = new Set<string>();
159+
while (cursor !== null) {
160+
if (cursors.has(cursor)) return null;
161+
cursors.add(cursor);
162+
const page: PullRequestThreadCommentsResult = yield* pullRequests.threadComments({
163+
...reference,
164+
threadId: thread.id,
165+
cursor,
156166
});
167+
for (const comment of page.comments) {
168+
comments.set(comment.id, {
169+
...comment,
170+
kind: "review-comment",
171+
path: thread.path,
172+
reviewState: null,
173+
});
174+
}
175+
cursor = page.nextCursor;
157176
}
158-
cursor = page.nextCursor;
177+
if (thread.comments.length + comments.size < count) return null;
178+
tail = { count, comments: [...comments.values()] };
179+
tails.set(thread.id, tail);
159180
}
160-
if (ids.size < (thread.commentCount ?? 0)) return null;
181+
remarks.push(...tail.comments);
161182
}
162-
return [...comments.values()].sort((left, right) =>
163-
left.createdAt.localeCompare(right.createdAt),
164-
);
183+
return remarks.sort((left, right) => left.createdAt.localeCompare(right.createdAt));
165184
},
166185
Effect.catch((error) =>
167186
Effect.logWarning("pull request watch comment pagination failed", { error }).pipe(
@@ -206,7 +225,7 @@ export const make = Effect.gen(function* () {
206225
if (detail.state !== "open") return yield* record(target, null);
207226

208227
// Never advance the remark watermark past comments an incomplete read could have missed.
209-
const remarks = yield* readRemarks(reference, activity);
228+
const remarks = yield* readRemarks(target, reference, activity);
210229
const report = evaluatePullRequestWatch(watch, detail, remarks);
211230
if (report.changes.length > 0) {
212231
return yield* record(
@@ -233,6 +252,7 @@ export const make = Effect.gen(function* () {
233252
);
234253
const keys = new Set(targets.map(failureKey));
235254
for (const key of readFailures.keys()) if (!keys.has(key)) readFailures.delete(key);
255+
for (const key of threadTails.keys()) if (!keys.has(key)) threadTails.delete(key);
236256
yield* Effect.forEach(
237257
targets,
238258
(target) =>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,7 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
24382438
};
24392439
const incomplete = mode !== "single page" && mode !== "paginated";
24402440
let recovering = false;
2441+
let pagesRead = 0;
24412442
const reactor = yield* PullRequestWatchReactor.make.pipe(
24422443
Effect.provide(
24432444
Layer.mergeAll(
@@ -2472,6 +2473,7 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
24722473
commits: [],
24732474
}),
24742475
threadComments: (input) => {
2476+
pagesRead += 1;
24752477
assert.equal(input.threadId, "review-thread");
24762478
if (input.cursor === "after-10") {
24772479
return Effect.succeed({ comments: [eleventh], nextCursor: "after-11" });
@@ -2514,7 +2516,10 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
25142516
recovering = true;
25152517
}
25162518
yield* reactor.sweep;
2519+
// A thread whose count has not moved is not paged again.
2520+
const pagesBefore = pagesRead;
25172521
yield* reactor.sweep;
2522+
assert.equal(pagesRead, pagesBefore);
25182523

25192524
const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]);
25202525
assert.deepEqual(

0 commit comments

Comments
 (0)