Skip to content

Commit 500391c

Browse files
committed
fix(server): read paginated review replies when watching PRs
1 parent 2a45557 commit 500391c

2 files changed

Lines changed: 166 additions & 30 deletions

File tree

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

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
CommandId,
33
MessageId,
44
type OrchestrationV2Notification,
5+
type PullRequestActivity,
6+
type PullRequestRef,
57
type ThreadPullRequestLink,
68
type ThreadPullRequestWatch,
79
} from "@t3tools/contracts";
@@ -125,6 +127,52 @@ export const make = Effect.gen(function* () {
125127
},
126128
}).pipe(Effect.catch(() => record(target, null)));
127129

130+
const readRemarks = Effect.fn("PullRequestWatchReactor.readRemarks")(
131+
function* (reference: PullRequestRef, activity: PullRequestActivity) {
132+
// Truncation without a continuation means the initial thread read was incomplete.
133+
if (
134+
activity.commentsTruncated &&
135+
!activity.reviewThreads.some((thread) => thread.nextCommentsCursor !== undefined)
136+
)
137+
return null;
138+
139+
const comments = new Map(activity.comments.map((comment) => [comment.id, comment]));
140+
for (const thread of activity.reviewThreads) {
141+
const ids = new Set(thread.comments.map((comment) => comment.id));
142+
const cursors = new Set<string>();
143+
let cursor = thread.nextCommentsCursor ?? null;
144+
while (cursor !== null) {
145+
if (cursors.has(cursor)) return null;
146+
cursors.add(cursor);
147+
const page = yield* pullRequests.threadComments({
148+
...reference,
149+
threadId: thread.id,
150+
cursor,
151+
});
152+
for (const comment of page.comments) {
153+
ids.add(comment.id);
154+
comments.set(comment.id, {
155+
...comment,
156+
kind: "review-comment",
157+
path: thread.path,
158+
reviewState: null,
159+
});
160+
}
161+
cursor = page.nextCursor;
162+
}
163+
if (ids.size < (thread.commentCount ?? 0)) return null;
164+
}
165+
return [...comments.values()].sort((left, right) =>
166+
left.createdAt.localeCompare(right.createdAt),
167+
);
168+
},
169+
Effect.catch((error) =>
170+
Effect.logWarning("pull request watch comment pagination failed", { error }).pipe(
171+
Effect.as(null),
172+
),
173+
),
174+
);
175+
128176
const check = Effect.fn("PullRequestWatchReactor.check")(function* (target: WatchTarget) {
129177
const { thread, link, watch } = target;
130178
const pullRequest = identityOf(link);
@@ -160,13 +208,9 @@ export const make = Effect.gen(function* () {
160208
const [detail, activity] = read.value;
161209
if (detail.state !== "open") return yield* record(target, null);
162210

163-
// A degraded read (GitHub's review thread query failed) is truncated with no long thread to
164-
// explain it, and would skip review comments, so remarks wait for a later pass. Replies past
165-
// the first ten of a long review thread are not read.
166-
const degraded =
167-
activity.commentsTruncated &&
168-
!activity.reviewThreads.some((reviewThread) => reviewThread.nextCommentsCursor !== undefined);
169-
const report = evaluatePullRequestWatch(watch, detail, degraded ? null : activity.comments);
211+
// Never advance the remark watermark past comments an incomplete read could have missed.
212+
const remarks = yield* readRemarks(reference, activity);
213+
const report = evaluatePullRequestWatch(watch, detail, remarks);
170214
if (report.changes.length > 0) {
171215
return yield* record(
172216
target,

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

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
type OrchestrationV2Run,
1919
ProjectId,
2020
type PullRequestDetail,
21+
type PullRequestComment,
22+
PullRequestOperationError,
2123
ProviderDriverKind,
2224
ProviderInstanceId,
2325
ProviderThreadId,
@@ -2307,11 +2309,17 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
23072309
}),
23082310
);
23092311

2310-
it.effect("wakes a watched thread once for failed checks and a review comment", () =>
2312+
it.effect.each([
2313+
"single page",
2314+
"paginated",
2315+
"page failure",
2316+
"repeated cursor",
2317+
"missing comments",
2318+
])("wakes a watched thread once: %s", (mode) =>
23112319
Effect.gen(function* () {
23122320
const orchestrator = yield* Orchestrator.OrchestratorV2;
2313-
const threadId = ThreadId.make("runtime-pull-request-watch-wake");
2314-
const projectId = ProjectId.make("pr-watch-wake-project");
2321+
const threadId = ThreadId.make(`runtime-pull-request-watch-wake-${mode}`);
2322+
const projectId = ProjectId.make(`pr-watch-wake-project-${mode}`);
23152323
yield* seedProject({
23162324
projectId,
23172325
title: "Watch wake",
@@ -2323,7 +2331,7 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
23232331
type: "thread.create",
23242332
createdBy: "user",
23252333
creationSource: "web",
2326-
commandId: CommandId.make("pr-watch-wake-create"),
2334+
commandId: CommandId.make(`pr-watch-wake-create-${mode}`),
23272335
threadId,
23282336
projectId,
23292337
title: "Watch wake",
@@ -2337,15 +2345,15 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
23372345
const url = "https://github.com/pingdotgg/t3code/pull/7";
23382346
yield* orchestrator.dispatch({
23392347
type: "thread.pull-request.link",
2340-
commandId: CommandId.make("pr-watch-wake-link"),
2348+
commandId: CommandId.make(`pr-watch-wake-link-${mode}`),
23412349
threadId,
23422350
...key,
23432351
url,
23442352
source: "agent",
23452353
});
23462354
yield* orchestrator.dispatch({
23472355
type: "thread.pull-request.watch",
2348-
commandId: CommandId.make("pr-watch-wake-start"),
2356+
commandId: CommandId.make(`pr-watch-wake-start-${mode}`),
23492357
threadId,
23502358
...key,
23512359
watching: true,
@@ -2398,6 +2406,37 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
23982406
mergeCapabilities: { merge: true, squash: true, rebase: true },
23992407
viewer: "agent-user",
24002408
};
2409+
const initialWatch = (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch;
2410+
assert.isDefined(initialWatch);
2411+
const remark: PullRequestComment = {
2412+
id: "review-1",
2413+
kind: "review-comment",
2414+
author: { login: "reviewer", name: null, avatarUrl: null },
2415+
body: "One more thing.",
2416+
createdAt: "2999-01-01T00:00:03.000Z",
2417+
url: null,
2418+
path: "src/index.ts",
2419+
reviewState: null,
2420+
};
2421+
const firstTen = Array.from({ length: 10 }, (_, index) => ({
2422+
...remark,
2423+
id: `old-${index}`,
2424+
createdAt: "1900-01-01T00:00:00.000Z",
2425+
}));
2426+
const eleventh = {
2427+
...remark,
2428+
id: "reply-11",
2429+
body: "Eleventh reply.",
2430+
createdAt: "2999-01-01T00:00:01.000Z",
2431+
};
2432+
const twelfth = {
2433+
...remark,
2434+
id: "reply-12",
2435+
body: "Twelfth reply.",
2436+
createdAt: "2999-01-01T00:00:02.000Z",
2437+
};
2438+
const incomplete = mode !== "single page" && mode !== "paginated";
2439+
let recovering = false;
24012440
const reactor = yield* PullRequestWatchReactor.make.pipe(
24022441
Effect.provide(
24032442
Layer.mergeAll(
@@ -2406,27 +2445,70 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
24062445
detail: () => Effect.succeed(detail),
24072446
activity: () =>
24082447
Effect.succeed({
2409-
comments: [
2410-
{
2411-
id: "review-1",
2412-
kind: "review-comment",
2413-
author: { login: "reviewer", name: null, avatarUrl: null },
2414-
body: "One more thing.",
2415-
createdAt: "2999-01-01T00:00:00.000Z",
2416-
url: null,
2417-
path: "src/index.ts",
2418-
reviewState: null,
2419-
},
2420-
],
2421-
commentCount: 1,
2422-
commentsTruncated: false,
2423-
reviewThreads: [],
2448+
comments:
2449+
mode === "single page"
2450+
? [remark]
2451+
: [...firstTen, { ...remark, kind: "issue-comment" }],
2452+
commentCount: mode === "single page" ? 1 : 13,
2453+
commentsTruncated: mode !== "single page",
2454+
reviewThreads:
2455+
mode === "single page"
2456+
? []
2457+
: [
2458+
{
2459+
id: "review-thread",
2460+
path: "src/index.ts",
2461+
line: 1,
2462+
side: "right",
2463+
isResolved: false,
2464+
isOutdated: false,
2465+
comments: firstTen,
2466+
commentCount: 12,
2467+
nextCommentsCursor: "after-10",
2468+
},
2469+
],
24242470
commits: [],
24252471
}),
2472+
threadComments: (input) => {
2473+
assert.equal(input.threadId, "review-thread");
2474+
if (input.cursor === "after-10") {
2475+
return Effect.succeed({ comments: [eleventh], nextCursor: "after-11" });
2476+
}
2477+
assert.equal(input.cursor, "after-11");
2478+
if (!recovering && mode === "page failure") {
2479+
return Effect.fail(
2480+
new PullRequestOperationError({
2481+
operation: "threadComments",
2482+
detail: "Page unavailable",
2483+
}),
2484+
);
2485+
}
2486+
if (!recovering && mode === "repeated cursor") {
2487+
return Effect.succeed({ comments: [eleventh], nextCursor: "after-11" });
2488+
}
2489+
if (!recovering && mode === "missing comments") {
2490+
return Effect.succeed({ comments: [], nextCursor: null });
2491+
}
2492+
// Overlapping pages must not report the same reply twice.
2493+
return Effect.succeed({ comments: [eleventh, twelfth], nextCursor: null });
2494+
},
24262495
}),
24272496
),
24282497
),
24292498
);
2499+
if (incomplete) {
2500+
yield* reactor.sweep;
2501+
const held = (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch;
2502+
assert.equal(held?.remarksThrough, initialWatch?.remarksThrough);
2503+
assert.deepEqual(held?.remarkIds, initialWatch?.remarkIds);
2504+
assert.deepEqual(held?.failedChecks, ["lint"]);
2505+
const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]);
2506+
assert.deepEqual(
2507+
messages.map((message) => message.notification?.summary),
2508+
["#7: checks failed"],
2509+
);
2510+
recovering = true;
2511+
}
24302512
yield* reactor.sweep;
24312513
yield* reactor.sweep;
24322514

@@ -2435,12 +2517,22 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
24352517
messages.flatMap((message) =>
24362518
message.notification === undefined ? [] : [message.notification.summary],
24372519
),
2438-
["#7: checks failed, new comments"],
2520+
incomplete
2521+
? ["#7: checks failed", "#7: new comments"]
2522+
: ["#7: checks failed, new comments"],
24392523
);
2524+
if (mode !== "single page") {
2525+
const wake = messages.at(-1);
2526+
assert.include(wake?.text ?? "", "3 new comments");
2527+
assert.include(wake?.text ?? "", "Eleventh reply.");
2528+
assert.include(wake?.text ?? "", "Twelfth reply.");
2529+
}
24402530
const watch = (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch;
2531+
assert.equal(watch?.remarksThrough, remark.createdAt);
2532+
assert.deepEqual(watch?.remarkIds, [remark.id]);
24412533
assert.deepEqual(
24422534
{ headSha: watch?.headSha, failedChecks: watch?.failedChecks, wakes: watch?.wakes },
2443-
{ headSha: "abc1234def", failedChecks: ["lint"], wakes: 0 },
2535+
{ headSha: "abc1234def", failedChecks: ["lint"], wakes: incomplete ? 1 : 0 },
24442536
);
24452537
}),
24462538
);

0 commit comments

Comments
 (0)