The best way to control Claude Code, Codex and more from any computer or phone
fix(prs): refresh viewed files after partial batch failures (#14954)
Co-authored-by: SAPHID <saphid@users.noreply.github.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 07:21 UTC
9f1005a17ab451da112e961f8815522888edcf97
6 files changed
+189
-38
apps/server/src/pullRequest/PullRequestService.test.ts
+59
@@ -6214,6 +6214,65 @@ it.effect("names the signed-in account in the detail, and says nothing where the
6214
}),
6215
);
6216
6217
+it.effect("partial host viewed writes invalidate marks but keep the diff cached", () =>
6218
+ Effect.gen(function* () {
6219
+ let diffReads = 0;
6220
+ const states = new Map<string, "viewed" | "unviewed">([
6221
+ ["a.ts", "unviewed"],
6222
+ ["b.ts", "unviewed"],
6223
+ ]);
6224
+ const service = yield* makeService({
6225
+ projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
6226
+ providers: [
6227
+ fakeProvider(SourceControlProviderKind.make("github"), {
6228
+ capabilities: {
6229
+ ...fakeProvider(SourceControlProviderKind.make("github")).capabilities,
6230
+ viewedFiles: "host",
6231
+ },
6232
+ getDiff: () =>
6233
+ Effect.sync(() => {
6234
+ diffReads += 1;
6235
+ return { patch: "@@", truncated: false, nextCursor: null };
6236
+ }),
6237
+ getFilesViewed: () =>
6238
+ Effect.sync(() => ({
6239
+ files: [...states].map(([path, state]) => ({ path, state })),
6240
+ truncated: false,
6241
+ })),
6242
+ setFilesViewed: () =>
6243
+ Effect.gen(function* () {
6244
+ states.set("a.ts", "viewed");
6245
+ return yield* new PullRequestProviderError({
6246
+ provider: SourceControlProviderKind.make("github"),
6247
+ operation: "setFilesViewed",
6248
+ reason: "failed",
6249
+ detail: "Second mutation refused",
6250
+ });
6251
+ }),
6252
+ }),
6253
+ ],
6254
+ });
6255
+ const ref = { projectId: "p1" as ProjectId, repository: "acme/web", number: 10 };
6256
+ yield* service.diff(ref);
6257
+ assert.deepStrictEqual((yield* service.filesViewed(ref)).files, [
6258
+ { path: "a.ts", state: "unviewed" },
6259
+ { path: "b.ts", state: "unviewed" },
6260
+ ]);
6261
+ yield* Effect.flip(
6262
+ service.setFilesViewed({
6263
+ ...ref,
6264
+ files: [...states.keys()].map((path) => ({ path, viewed: true })),
6265
+ }),
6266
+ );
6267
+ yield* service.diff(ref);
6268
+ assert.strictEqual(diffReads, 1);
6269
+ assert.deepStrictEqual((yield* service.filesViewed(ref)).files, [
6270
+ { path: "a.ts", state: "viewed" },
6271
+ { path: "b.ts", state: "unviewed" },
6272
+ ]);
6273
+ }),
6274
+);
6275
+
6276
it.effect("keeps the diff cached across a file being ticked off", () =>
6277
Effect.gen(function* () {
6278
let diffReads = 0;
apps/server/src/pullRequest/PullRequestService.ts
+1
-1
@@ -1771,7 +1771,7 @@ export const make = Effect.gen(function* () {
1771
// Deliberately not `invalidatedByMutation`: ticking a file off says nothing about the
1772
// change request, and dropping a 300-file diff on every checkbox is the whole cost of
1773
// the feature.
1774
- Effect.tap(() => Effect.sync(() => bumpFilesViewedEpoch(ref))),
1774
+ Effect.ensuring(Effect.sync(() => bumpFilesViewedEpoch(ref))),
1775
),
1776
),
1777
);
apps/web/src/components/pullRequest/usePullRequestFilesViewed.browser.test.tsx
+31
-1
@@ -4,6 +4,7 @@ import {
4
type PullRequestFilesViewedResult,
5
type PullRequestRef,
6
} from "@t3tools/contracts";
7
+import * as Cause from "effect/Cause";
8
import { AsyncResult } from "effect/reactivity";
9
import { act, StrictMode, useEffect } from "react";
10
import { createRoot, type Root } from "react-dom/client";
@@ -47,7 +48,13 @@ const reference: PullRequestRef = {
48
};
49
50
function answer(state: "unviewed" | "viewed" | "dismissed"): PullRequestFilesViewedResult {
50
- return { files: [{ path: "a.ts", state }], truncated: false };
51
+ return {
52
+ files: [
53
+ { path: "a.ts", state },
54
+ { path: "b.ts", state: "unviewed" },
55
+ ],
56
+ truncated: false,
57
+ };
58
}
59
60
let root: Root;
@@ -148,3 +155,26 @@ describe("a mark the host has not answered for yet", () => {
155
expect(view().isStale("a.ts")).toBe(false);
156
});
157
});
158
+
159
+describe("a failed batch that the host partly applied", () => {
160
+ it("refreshes the host and accepts the applied marks", async () => {
161
+ setFilesViewed.mockResolvedValueOnce(
162
+ AsyncResult.failure(Cause.fail(new Error("partial batch"))),
163
+ );
164
+ host.refresh.mockImplementation(() => {
165
+ host.data = answer("viewed");
166
+ root.render(
167
+ <StrictMode>
168
+ <Surface />
169
+ </StrictMode>,
170
+ );
171
+ });
172
+ act(() => {
173
+ view().setViewed("a.ts", true);
174
+ view().setViewed("b.ts", true);
175
+ });
176
+ await act(async () => vi.advanceTimersByTimeAsync(500));
177
+ expect(view().isViewed("a.ts")).toBe(true);
178
+ expect(view().isViewed("b.ts")).toBe(false);
179
+ });
180
+});
apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts
+1
@@ -107,6 +107,7 @@ export function usePullRequestFilesViewed(options: {
107
setOverlay((current) => revertFileViewedOverlay(current, batch, owned));
108
if (owned.size > 0 && !isAtomCommandInterrupted(result)) {
109
toastManager.add({ type: "error", title: "Could not update viewed files" });
110
+ refresh();
111
}
112
return;
113
}
packages/client-runtime/src/state/pullRequestRouting.ts
+33
-36
@@ -202,7 +202,7 @@ const invalidateTarget = Effect.fn("PullRequestRouting.invalidateTarget")(functi
202
inputs,
203
(input) =>
204
registry.run(target, request(WS_METHODS.pullRequestsInvalidate, input)).pipe(
205
- // GitHub already accepted the write. A stalled reader must not delay its confirmation.
205
+ // A stalled reader must not delay the write's answer.
206
Effect.timeoutOption("1 second"),
207
Effect.orElseSucceed(() => undefined),
208
),
@@ -272,42 +272,39 @@ export function createPullRequestRouter() {
272
ref.repository.toLowerCase(),
273
String(ref.number),
274
]);
275
- const finish = (operation: ReturnType<typeof request<T>>) =>
276
- operation.pipe(
277
- Effect.tap(() => {
278
- if (!writes.has(tag) || used.size === 0) return Effect.void;
279
- const targets = new Map<EnvironmentId, PullRequestRef[]>([
280
- [origin.target.environmentId, [ref]],
281
- ]);
282
- for (const entry of used.values()) {
283
- if (
284
- entry.origin !== origin.target.environmentId ||
285
- !matchesReference(entry.reference, ref)
286
- )
287
- continue;
288
- for (const target of [...entry.targets, origin.target.environmentId]) {
289
- const refs = targets.get(target) ?? [];
290
- if (!refs.some((existing) => existing.host === entry.reference.host))
291
- refs.push(entry.reference);
292
- targets.set(target, refs);
293
- }
294
- }
295
- return Effect.forEach(
296
- targets,
297
- ([target, refs]) =>
298
- invalidateTarget(registry, origin.target.environmentId, target, [
299
- ...refs.map((reference) => ({
300
- reference,
301
- ...(tag === WS_METHODS.pullRequestsSetFilesViewed
302
- ? { filesViewedOnly: true }
303
- : {}),
304
- })),
305
- ...(tag === WS_METHODS.pullRequestsSetFilesViewed ? [] : [{}]),
306
- ]),
307
- { concurrency: 4, discard: true },
308
- );
309
- }),
275
+ const invalidateReaders = Effect.suspend(() => {
276
+ if (!writes.has(tag) || used.size === 0) return Effect.void;
277
+ const targets = new Map<EnvironmentId, PullRequestRef[]>([
278
+ [origin.target.environmentId, [ref]],
279
+ ]);
280
+ for (const entry of used.values()) {
281
+ if (entry.origin !== origin.target.environmentId || !matchesReference(entry.reference, ref))
282
+ continue;
283
+ for (const target of [...entry.targets, origin.target.environmentId]) {
284
+ const refs = targets.get(target) ?? [];
285
+ if (!refs.some((existing) => existing.host === entry.reference.host))
286
+ refs.push(entry.reference);
287
+ targets.set(target, refs);
288
+ }
289
+ }
290
+ return Effect.forEach(
291
+ targets,
292
+ ([target, refs]) =>
293
+ invalidateTarget(registry, origin.target.environmentId, target, [
294
+ ...refs.map((reference) => ({
295
+ reference,
296
+ ...(tag === WS_METHODS.pullRequestsSetFilesViewed ? { filesViewedOnly: true } : {}),
297
+ })),
298
+ ...(tag === WS_METHODS.pullRequestsSetFilesViewed ? [] : [{}]),
299
+ ]),
300
+ { concurrency: 4, discard: true },
301
);
302
+ });
303
+ // A failed viewed-files batch may have applied some paths. Other writes invalidate on success.
304
+ const finish = (operation: ReturnType<typeof request<T>>) =>
305
+ tag === WS_METHODS.pullRequestsSetFilesViewed
306
+ ? operation.pipe(Effect.ensuring(invalidateReaders))
307
+ : operation.pipe(Effect.tap(() => invalidateReaders));
308
const alternatives = [];
309
for (const [id, entry] of entries) {
310
if (id === origin.target.environmentId) continue;
packages/client-runtime/src/state/pullRequests.test.ts
+64
@@ -584,6 +584,70 @@ it.effect.each(["github", "gitlab", "bitbucket", "azure-devops"] as const)(
584
),
585
);
586
587
+it.effect("forgets routed viewed marks when a viewed-files batch fails", () =>
588
+ Effect.scoped(
589
+ Effect.gen(function* () {
590
+ const reference = {
591
+ projectId: ProjectId.make("project-1"),
592
+ host: "github.com",
593
+ repository: "acme/web",
594
+ number: 7,
595
+ };
596
+ const invalidations: { environment: string; input: unknown }[] = [];
597
+ const identity = () =>
598
+ Effect.succeed({
599
+ host: reference.host,
600
+ provider: "github",
601
+ accountId: "123",
602
+ viewer: "maria-rcks",
603
+ });
604
+ const clientFor = (environment: string) =>
605
+ ({
606
+ [WS_METHODS.pullRequestsRouting]: identity,
607
+ [WS_METHODS.pullRequestsRoutingIdentity]: identity,
608
+ [WS_METHODS.pullRequestsFilesViewed]: () =>
609
+ Effect.succeed({ files: [{ path: "a.ts", state: "unviewed" }], truncated: false }),
610
+ // The host may have applied part of the batch before refusing the rest.
611
+ [WS_METHODS.pullRequestsSetFilesViewed]: () => Effect.fail("second path refused"),
612
+ [WS_METHODS.pullRequestsInvalidate]: (input: unknown) =>
613
+ Effect.sync(() => {
614
+ invalidations.push({ environment, input });
615
+ }),
616
+ }) as unknown as WsRpcProtocolClient;
617
+ const { environmentRegistry, supervisor } = yield* makeTestRuntime(
618
+ clientFor("origin"),
619
+ clientFor("local"),
620
+ );
621
+ const route = createPullRequestRouter();
622
+ const write = yield* Effect.gen(function* () {
623
+ yield* route(WS_METHODS.pullRequestsFilesViewed, reference);
624
+ return yield* Effect.exit(
625
+ route(WS_METHODS.pullRequestsSetFilesViewed, {
626
+ ...reference,
627
+ files: [
628
+ { path: "a.ts", viewed: true },
629
+ { path: "b.ts", viewed: true },
630
+ ],
631
+ }),
632
+ );
633
+ }).pipe(
634
+ Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
635
+ Effect.provideService(GitHubRoutingPermissions, trustedRouting),
636
+ Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
637
+ Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
638
+ );
639
+ expect(write._tag).toBe("Failure");
640
+ expect(invalidations).toContainEqual({
641
+ environment: "local",
642
+ input: { reference: expect.objectContaining(reference), filesViewedOnly: true },
643
+ });
644
+ for (const call of invalidations) {
645
+ expect(call.input).toMatchObject({ filesViewedOnly: true });
646
+ }
647
+ }),
648
+ ),
649
+);
650
+
651
const TARGET = new PrimaryConnectionTarget({
652
environmentId: EnvironmentId.make("environment-1"),
653
label: "Test environment",