The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 00:30 UTC
fix(server): refresh cached PR review replies (#18835)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Alex Southwell committed
Oct 20, 2026 at 02:50 UTC
965a366e57199805896e6525159672b140096c59
2 files changed
+316
-14
apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
+74
-14
@@ -245,7 +245,14 @@ export const make = Effect.gen(function* () {
245
const lastReads = new Map<string, LastRead>();
246
const threadTails = new Map<
247
string,
248
- Map<string, { readonly count: number; readonly comments: ReadonlyArray<PullRequestComment> }>
248
+ {
249
+ readonly fetchedAt: number;
250
+ readonly visibleThrough: number;
251
+ readonly tails: ReadonlyMap<
252
+ string,
253
+ { readonly count: number; readonly comments: ReadonlyArray<PullRequestComment> }
254
+ >;
255
+ }
256
>();
257
258
const identityOf = (link: ThreadPullRequestLink) => ({
@@ -294,23 +301,45 @@ export const make = Effect.gen(function* () {
301
);
302
303
const readRemarks = Effect.fn("PullRequestWatchReactor.readRemarks")(
297
- function* (key: string, reference: PullRequestRef, activity: PullRequestActivity) {
304
+ function* (
305
+ key: string,
306
+ reference: PullRequestRef,
307
+ activity: PullRequestActivity,
308
+ now: number,
309
+ rereadMs: number,
310
+ ) {
311
// Comment cursors cannot account for missing threads. Only finish a truncated read
312
// when the host confirms that every thread was listed.
313
if (activity.commentsTruncated && activity.reviewThreadsTruncated !== false) return null;
314
302
- let tails = threadTails.get(key);
303
- if (tails === undefined) {
304
- tails = new Map();
305
- threadTails.set(key, tails);
306
- }
315
+ const visibleThrough = activity.comments.reduce(
316
+ (latest, comment) => Math.max(latest, Date.parse(comment.editedAt ?? comment.createdAt)),
317
+ 0,
318
+ );
319
+ const paginated = activity.reviewThreads.filter(
320
+ (thread) => thread.nextCommentsCursor != null,
321
+ );
322
+ const cached = threadTails.get(key);
323
+ // All tails share one watermark, so newer visible remarks or a changed thread must
324
+ // refresh the entire group before any remarks can advance it.
325
+ const reuse =
326
+ cached !== undefined &&
327
+ now - cached.fetchedAt < rereadMs &&
328
+ cached.visibleThrough === visibleThrough &&
329
+ cached.tails.size === paginated.length &&
330
+ paginated.every(
331
+ (thread) => cached.tails.get(thread.id)?.count === (thread.commentCount ?? 0),
332
+ );
333
+ const tails = new Map<
334
+ string,
335
+ { count: number; comments: ReadonlyArray<PullRequestComment> }
336
+ >();
337
const remarks = [...activity.comments];
308
- for (const thread of activity.reviewThreads) {
338
+ for (const thread of paginated) {
339
let cursor = thread.nextCommentsCursor ?? null;
310
- if (cursor === null) continue;
340
const count = thread.commentCount ?? 0;
312
- let tail = tails.get(thread.id);
313
- if (tail?.count !== count) {
341
+ let tail = reuse ? cached.tails.get(thread.id) : undefined;
342
+ if (tail === undefined) {
343
const comments = new Map<string, PullRequestComment>();
344
const cursors = new Set<string>();
345
while (cursor !== null) {
@@ -333,10 +362,15 @@ export const make = Effect.gen(function* () {
362
}
363
if (thread.comments.length + comments.size < count) return null;
364
tail = { count, comments: [...comments.values()] };
336
- tails.set(thread.id, tail);
365
}
366
+ tails.set(thread.id, tail);
367
remarks.push(...tail.comments);
368
}
369
+ threadTails.set(key, {
370
+ fetchedAt: reuse ? cached.fetchedAt : now,
371
+ visibleThrough,
372
+ tails,
373
+ });
374
return remarks.sort((left, right) => left.createdAt.localeCompare(right.createdAt));
375
},
376
Effect.catch((error) =>
@@ -344,6 +378,15 @@ export const make = Effect.gen(function* () {
378
Effect.as(null),
379
),
380
),
381
+ (effect, key) =>
382
+ effect.pipe(
383
+ Effect.onExit((exit) =>
384
+ Effect.sync(() => {
385
+ // A retry must refresh every tail before advancing the shared remark watermark.
386
+ if (Exit.isFailure(exit) || exit.value === null) threadTails.delete(key);
387
+ }),
388
+ ),
389
+ ),
390
);
391
392
const closed = (target: WatchTarget) =>
@@ -413,6 +456,14 @@ export const make = Effect.gen(function* () {
456
const last = lastReads.get(group.key);
457
const plan = planRead(group, last, fingerprint, now);
458
if (!plan.detail) return;
459
+ if (
460
+ last === undefined ||
461
+ group.targets.some((target) => !last.watches.has(watchKey(target))) ||
462
+ (fingerprint !== null && last.remarks !== fingerprint.remarks)
463
+ ) {
464
+ // A new watch or changed host remarks needs a fresh view of every tail.
465
+ threadTails.delete(group.key);
466
+ }
467
const reference = referenceOf(group);
468
// The reads are recorded against this fingerprint, so a cached answer older than it must not
469
// stand in for them. Clients are not told: this only drops this pull request's cached reads.
@@ -465,10 +516,19 @@ export const make = Effect.gen(function* () {
516
517
// Never advance the remark watermark past comments an incomplete read could have missed.
518
const remarks = Option.isSome(activity)
468
- ? yield* readRemarks(group.key, reference, activity.value)
519
+ ? yield* readRemarks(
520
+ group.key,
521
+ reference,
522
+ activity.value,
523
+ now,
524
+ fingerprint === null ? QUIET_REREAD_MS : FINGERPRINT_REREAD_MS,
525
+ )
526
: null;
527
lastReads.set(group.key, {
471
- at: plan.activity || last === undefined ? now : last.at,
528
+ at:
529
+ plan.activity || last === undefined
530
+ ? (threadTails.get(group.key)?.fetchedAt ?? now)
531
+ : last.at,
532
fingerprint: group.fingerprint,
533
inFlight:
534
detail.mergeability === "unknown" ||
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+242
@@ -3356,6 +3356,248 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
3356
}),
3357
);
3358
3359
+ it.effect.each(["deadline", "fingerprint lost", "fingerprint changed", "mixed-age remarks"])(
3360
+ "delivers edited cached review replies: %s",
3361
+ (mode) =>
3362
+ Effect.gen(function* () {
3363
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
3364
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
3365
+ const projectId = ProjectId.make(`pr-watch-tail-project-${mode}`);
3366
+ const threadId = ThreadId.make(`runtime-pr-watch-tail-${mode}`);
3367
+ const startedAt = "2026-10-02T12:00:00.000Z";
3368
+ yield* TestClock.setTime(Date.parse(startedAt));
3369
+ yield* seedProject({
3370
+ projectId,
3371
+ title: "Watch edited replies",
3372
+ workspaceRoot: "/workspace/watch-tail",
3373
+ defaultModelSelection: null,
3374
+ createdAt: startedAt,
3375
+ });
3376
+ yield* orchestrator.dispatch({
3377
+ type: "thread.create",
3378
+ createdBy: "user",
3379
+ creationSource: "web",
3380
+ commandId: CommandId.make(`pr-watch-tail-create-${mode}`),
3381
+ threadId,
3382
+ projectId,
3383
+ title: "Watch edited replies",
3384
+ modelSelection,
3385
+ runtimeMode: "full-access",
3386
+ interactionMode: "default",
3387
+ branch: null,
3388
+ worktreePath: null,
3389
+ });
3390
+ yield* orchestrator.dispatch({
3391
+ type: "thread.pull-request.watch",
3392
+ commandId: CommandId.make(`pr-watch-tail-start-${mode}`),
3393
+ threadId,
3394
+ host: "github.com",
3395
+ repository: "pingdotgg/t3code",
3396
+ number: 12,
3397
+ watching: true,
3398
+ link: { url: "https://github.com/pingdotgg/t3code/pull/12", source: "agent" },
3399
+ });
3400
+ const reply: PullRequestComment = {
3401
+ id: "reply-11",
3402
+ kind: "review-comment",
3403
+ author: { login: "reviewer", name: null, avatarUrl: null },
3404
+ body: "Original feedback.",
3405
+ createdAt: "2026-10-02T11:00:00.000Z",
3406
+ url: null,
3407
+ path: "src/index.ts",
3408
+ reviewState: null,
3409
+ };
3410
+ let replies = Array.from(
3411
+ { length: mode === "fingerprint changed" ? 2 : 1 },
3412
+ (_, index) => ({
3413
+ ...reply,
3414
+ id: `reply-${index}-11`,
3415
+ }),
3416
+ );
3417
+ const reviewThreads = replies.map((reply, index) => ({
3418
+ id: `review-thread-${index}`,
3419
+ path: "src/index.ts",
3420
+ line: index + 1,
3421
+ side: "right" as const,
3422
+ isResolved: false,
3423
+ isOutdated: false,
3424
+ comments: Array.from({ length: 10 }, (_, index) => ({
3425
+ ...reply,
3426
+ id: `${reply.id}-preview-${index}`,
3427
+ })),
3428
+ commentCount: 11,
3429
+ nextCommentsCursor: "after-10",
3430
+ }));
3431
+ let fingerprint: string | null = mode === "mixed-age remarks" ? null : "before";
3432
+ let failPage = false;
3433
+ let activityReads = 0;
3434
+ const pageReads: Array<{ threadId: string; cursor: string }> = [];
3435
+ const expectedPages = reviewThreads.map((thread) => ({
3436
+ threadId: thread.id,
3437
+ cursor: "after-10",
3438
+ }));
3439
+ const reactor = yield* PullRequestWatchReactor.make.pipe(
3440
+ Effect.provide(
3441
+ Layer.mergeAll(
3442
+ NodeServices.layer,
3443
+ Layer.mock(ProjectionStore.ProjectionStoreV2)({
3444
+ getThreadsWithPullRequests: () =>
3445
+ projections
3446
+ .getThreadsWithPullRequests()
3447
+ .pipe(
3448
+ Effect.map((threads) => threads.filter((thread) => thread.id === threadId)),
3449
+ ),
3450
+ }),
3451
+ Layer.mock(PullRequestService.PullRequestService)({
3452
+ watchFingerprint: () =>
3453
+ Effect.succeed(
3454
+ fingerprint === null ? null : { status: "OPEN pending", remarks: fingerprint },
3455
+ ),
3456
+ invalidate: () => Effect.void,
3457
+ detail: () =>
3458
+ Effect.succeed({
3459
+ ...watchedPullRequestDetail({ projectId, number: 12, at: startedAt }),
3460
+ checks: [{ name: "test", status: "pending", description: null, url: null }],
3461
+ }),
3462
+ activity: () =>
3463
+ Effect.sync(() => {
3464
+ activityReads += 1;
3465
+ return {
3466
+ comments: reviewThreads.flatMap((thread) => thread.comments),
3467
+ commentCount: reviewThreads.length * 11,
3468
+ commentsTruncated: true,
3469
+ reviewThreadsTruncated: false,
3470
+ reviewThreads,
3471
+ commits: [],
3472
+ };
3473
+ }),
3474
+ threadComments: (input) =>
3475
+ Effect.suspend(() => {
3476
+ const index = reviewThreads.findIndex((thread) => thread.id === input.threadId);
3477
+ assert.isAtLeast(index, 0);
3478
+ pageReads.push({ threadId: input.threadId, cursor: input.cursor });
3479
+ return failPage && index === 1
3480
+ ? Effect.fail(
3481
+ new PullRequestOperationError({
3482
+ operation: "threadComments",
3483
+ detail: "Page unavailable",
3484
+ }),
3485
+ )
3486
+ : Effect.succeed({ comments: [replies[index]!], nextCursor: null });
3487
+ }),
3488
+ }),
3489
+ ),
3490
+ ),
3491
+ );
3492
+ const messages = orchestrator
3493
+ .getThreadRecords(threadId, ["messages"])
3494
+ .pipe(Effect.map((records) => records.messages));
3495
+ const watch = orchestrator
3496
+ .getThreadShell(threadId)
3497
+ .pipe(Effect.map((thread) => thread?.pullRequests?.[0]?.watch));
3498
+ yield* reactor.sweep;
3499
+ const baseline = yield* watch;
3500
+ replies = replies.map((reply) => ({
3501
+ ...reply,
3502
+ body: "Edited feedback.",
3503
+ editedAt: "2026-10-02T12:00:01.000Z",
3504
+ }));
3505
+
3506
+ if (mode === "mixed-age remarks") {
3507
+ const thread = reviewThreads[0]!;
3508
+ const visibleReply = {
3509
+ ...thread.comments[0]!,
3510
+ body: "Edited visible feedback.",
3511
+ editedAt: "2026-10-02T12:02:00.000Z",
3512
+ };
3513
+ thread.comments[0] = visibleReply;
3514
+ yield* TestClock.adjust("5 minutes");
3515
+ yield* reactor.sweep;
3516
+ const intermediateWatch = yield* watch;
3517
+ const intermediatePageReads = [...pageReads];
3518
+ yield* TestClock.adjust("5 minutes");
3519
+ yield* reactor.sweep;
3520
+ const wakes = yield* messages;
3521
+ assert.isTrue(wakes.every((wake) => wake.notification?.summary === "#12: new comments"));
3522
+ assert.equal((yield* watch)?.remarksThrough, visibleReply.editedAt);
3523
+ assert.deepEqual((yield* watch)?.remarkIds, [visibleReply.id]);
3524
+ assert.deepEqual(pageReads, [...expectedPages, ...expectedPages]);
3525
+ const wakeText = wakes.map((wake) => wake.text).join("\n");
3526
+ for (const reply of [visibleReply, ...replies]) {
3527
+ assert.include(wakeText, reply.body);
3528
+ assert.equal(wakeText.split(reply.body).length - 1, 1);
3529
+ }
3530
+ // A watermark past the deep edit requires a fresh tail, even before its deadline.
3531
+ if (intermediateWatch?.remarksThrough === visibleReply.editedAt) {
3532
+ assert.deepEqual(intermediatePageReads, [...expectedPages, ...expectedPages]);
3533
+ } else {
3534
+ assert.equal(intermediateWatch?.remarksThrough, baseline?.remarksThrough);
3535
+ }
3536
+ yield* reactor.sweep;
3537
+ yield* TestClock.adjust("30 minutes");
3538
+ yield* reactor.sweep;
3539
+ assert.deepEqual(yield* messages, wakes);
3540
+ assert.equal((yield* watch)?.remarksThrough, visibleReply.editedAt);
3541
+ assert.deepEqual(pageReads, [...expectedPages, ...expectedPages, ...expectedPages]);
3542
+ return;
3543
+ }
3544
+
3545
+ if (mode === "fingerprint changed") {
3546
+ yield* TestClock.adjust("1 minute");
3547
+ fingerprint = "after";
3548
+ failPage = true;
3549
+ yield* reactor.sweep;
3550
+ assert.deepEqual(yield* messages, []);
3551
+ assert.deepEqual(yield* watch, baseline);
3552
+ assert.deepEqual(pageReads, [...expectedPages, ...expectedPages]);
3553
+ yield* TestClock.adjust("2 seconds");
3554
+ // Neither deep edit changes the host fingerprint or the thread's comment count.
3555
+ replies = replies.map((reply, index) => ({
3556
+ ...reply,
3557
+ body: `Latest feedback ${index + 1}.`,
3558
+ editedAt: `2026-10-02T12:01:0${index + 1}.000Z`,
3559
+ }));
3560
+ failPage = false;
3561
+ } else {
3562
+ if (mode === "fingerprint lost") fingerprint = null;
3563
+ yield* TestClock.adjust(mode === "deadline" ? "29 minutes" : "5 minutes");
3564
+ yield* reactor.sweep;
3565
+ assert.deepEqual(pageReads, expectedPages);
3566
+ assert.deepEqual(yield* messages, []);
3567
+ // An activity read at five minutes must not move the tail's ten-minute deadline.
3568
+ assert.equal(activityReads, mode === "deadline" ? 1 : 2);
3569
+ yield* TestClock.adjust(mode === "deadline" ? "1 minute" : "5 minutes");
3570
+ }
3571
+ yield* reactor.sweep;
3572
+ const wakes = yield* messages;
3573
+ assert.deepEqual(
3574
+ wakes.map((message) => message.notification?.summary),
3575
+ ["#12: new comments"],
3576
+ );
3577
+ for (const reply of replies) {
3578
+ assert.include(wakes[0]?.text ?? "", reply.body);
3579
+ }
3580
+ assert.notInclude(wakes[0]?.text ?? "", "Original feedback.");
3581
+ assert.deepEqual(
3582
+ pageReads,
3583
+ Array.from(
3584
+ { length: mode === "fingerprint changed" ? 3 : 2 },
3585
+ () => expectedPages,
3586
+ ).flat(),
3587
+ );
3588
+ const latestReply = replies.at(-1)!;
3589
+ assert.equal((yield* watch)?.remarksThrough, latestReply.editedAt);
3590
+ assert.deepEqual((yield* watch)?.remarkIds, [latestReply.id]);
3591
+ yield* reactor.sweep;
3592
+ assert.lengthOf(yield* messages, 1);
3593
+ assert.lengthOf(pageReads, mode === "fingerprint changed" ? 6 : 2);
3594
+ yield* TestClock.adjust("30 minutes");
3595
+ yield* reactor.sweep;
3596
+ assert.lengthOf(yield* messages, 1);
3597
+ assert.lengthOf(pageReads, mode === "fingerprint changed" ? 8 : 3);
3598
+ }),
3599
+ );
3600
+
3601
it.effect.each([
3602
"single page",
3603
"paginated",