@devries-cameron20 / t3code-1 / commits / 70be20493b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix: re-ticking a file after a push no longer shows it Viewed (#24151)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor committed Nov 14, 2026 at 01:55 UTC 70be20493b7769df4dd542851ecb2a0f872be59e
11 files changed +142 -69
apps/mobile/src/features/pull-requests/pullRequestFilesViewed.test.ts
+5 -13
@@ -1,24 +1,16 @@
1 -import type { PullRequestFilesViewedResult } from "@t3tools/contracts";
1 import { describe, expect, it } from "vite-plus/test";
2 import { viewedOverlay } from "./pullRequestFilesViewed";
3
5 -// The overlay compares reads by identity, so each call is a distinct read.
6 -const read = (): PullRequestFilesViewedResult => ({ files: [], truncated: false });
7 -
4 describe("viewedOverlay", () => {
5 it("keeps a press while its write is in flight", () => {
10 - const before = read();
6 expect(
12 - viewedOverlay({ "a.ts": { viewed: true, version: 1, awaitingReadAfter: undefined } }, before),
7 + viewedOverlay({ "a.ts": { viewed: true, version: 1, awaitingReadAfter: undefined } }, 3),
8 ).toEqual({ "a.ts": true });
9 });
10
16 - it("keeps a landed press until a read newer than the one current at the write", () => {
17 - const before = read();
18 - const presses = {
19 - "a.ts": { viewed: true, version: 1, awaitingReadAfter: { read: before } },
20 - };
21 - expect(viewedOverlay(presses, before)).toEqual({ "a.ts": true });
22 - expect(viewedOverlay(presses, read())).toEqual({});
11 + it("yields a landed press to the next completed read, even one with unchanged data", () => {
12 + const presses = { "a.ts": { viewed: true, version: 1, awaitingReadAfter: 3 } };
13 + expect(viewedOverlay(presses, 3)).toEqual({ "a.ts": true });
14 + expect(viewedOverlay(presses, 4)).toEqual({});
15 });
16 });
apps/mobile/src/features/pull-requests/pullRequestFilesViewed.ts
+4 -8
@@ -1,12 +1,8 @@
1 -import type { PullRequestFilesViewedResult } from "@t3tools/contracts";
2 -
3 -export type FilesViewedRead = PullRequestFilesViewedResult | null;
4 -
1 export interface ViewedPress {
2 readonly viewed: boolean;
3 readonly version: number;
8 - /** Set once the write lands: the press yields to the server once a read newer than this lands. */
9 - readonly awaitingReadAfter: { readonly read: FilesViewedRead } | undefined;
4 + /** Completed reads when the write landed: the press yields once a later read completes. */
5 + readonly awaitingReadAfter: number | undefined;
6 }
7
8 export interface ViewedState {
@@ -18,11 +14,11 @@ export interface ViewedState {
14 /** The pressed states the server's read has not caught up with yet. */
15 export function viewedOverlay(
16 presses: Readonly<Record<string, ViewedPress>>,
21 - read: FilesViewedRead,
17 + reads: number,
18 ): Record<string, boolean> {
19 const overlay: Record<string, boolean> = {};
20 for (const [path, press] of Object.entries(presses))
25 - if (press.awaitingReadAfter === undefined || press.awaitingReadAfter.read === read)
21 + if (press.awaitingReadAfter === undefined || press.awaitingReadAfter === reads)
22 overlay[path] = press.viewed;
23 return overlay;
24 }
apps/mobile/src/features/pull-requests/usePullRequestFilesViewed.ts
+11 -9
@@ -2,9 +2,12 @@ import { usePullRequestConnected } from "./usePullRequestConnected";
2 import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react";
3 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
4 import type { PullRequestDetail } from "@t3tools/contracts";
5 -import { AsyncResult } from "effect/reactivity";
5 import { use, useState } from "react";
7 -import { composerPullRequests } from "../../state/pull-requests";
6 +import {
7 + composerPullRequests,
8 + filesViewedReadsFamily,
9 + filesViewedReadsKey,
10 +} from "../../state/pull-requests";
11 import { useEnvironmentQuery } from "../../state/query";
12 import { useAtomCommand } from "../../state/use-atom-command";
13 import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
@@ -23,11 +26,13 @@ export function usePullRequestFilesViewed(params: PullRequestParams, pr: PullReq
26 const allowed = connected && permission;
27 const query = useEnvironmentQuery(supported ? composerPullRequests.filesViewed(target) : null);
28 const refresh = useAtomRefresh(composerPullRequests.filesViewed(target));
29 + const readsAtom = filesViewedReadsFamily(filesViewedReadsKey(target));
30 + const reads = useAtomValue(readsAtom);
31 const write = useAtomCommand(composerPullRequests.setFilesViewed, { reportFailure: false });
32 const [state, setState] = useState<ViewedState>({ key: scopeKey, presses: {}, error: null });
33 const presses = state.key === scopeKey ? state.presses : {};
34 const error = state.key === scopeKey ? state.error : null;
30 - const overlay = viewedOverlay(presses, query.data);
35 + const overlay = viewedOverlay(presses, reads);
36 const update = (change: (current: ViewedState) => ViewedState) =>
37 setState((current) =>
38 change(current.key === scopeKey ? current : { key: scopeKey, presses: {}, error: null }),
@@ -48,11 +53,8 @@ export function usePullRequestFilesViewed(params: PullRequestParams, pr: PullReq
53 ...target,
54 input: { ...target.input, files: [{ path, viewed }] },
55 });
51 - // The read that is current once the write lands still predates it; a newer one carries it.
52 - const readAtWrite = AsyncResult.getOrElse(
53 - registry.get(composerPullRequests.filesViewed(target)),
54 - () => null,
55 - );
56 + // A read completed by the time the write lands may predate it; the next one carries it.
57 + const readsAtWrite = registry.get(readsAtom);
58 setState((current) => {
59 if (current.key !== scopeKey || current.presses[path]?.version !== version) return current;
60 if (response._tag === "Success")
@@ -60,7 +62,7 @@ export function usePullRequestFilesViewed(params: PullRequestParams, pr: PullReq
62 ...current,
63 presses: {
64 ...current.presses,
63 - [path]: { viewed, version, awaitingReadAfter: { read: readAtWrite } },
65 + [path]: { viewed, version, awaitingReadAfter: readsAtWrite },
66 },
67 };
68 const rest = { ...current.presses };
apps/mobile/src/state/pull-requests.ts
+15 -1
@@ -3,7 +3,7 @@ import {
3 createPullRequestStackAtomFamily,
4 patchPullRequestListEntries,
5 } from "@t3tools/client-runtime/state/pull-requests";
6 -import type { EnvironmentId, PullRequestListInput } from "@t3tools/contracts";
6 +import type { EnvironmentId, PullRequestListInput, PullRequestRef } from "@t3tools/contracts";
7 import * as Effect from "effect/Effect";
8 import * as Option from "effect/Option";
9 import { AsyncResult, Atom } from "effect/reactivity";
@@ -41,6 +41,16 @@ export const pullRequestListStatsFamily = Atom.family((_environmentId: string) =
41 ),
42 );
43
44 +export const filesViewedReadsKey = (target: {
45 + readonly environmentId: EnvironmentId;
46 + readonly input: PullRequestRef;
47 +}) => JSON.stringify([target.environmentId, target.input]);
48 +
49 +/** Completed server reads of each viewed-files query; unchanged answers are structurally shared. */
50 +export const filesViewedReadsFamily = Atom.family((_key: string) =>
51 + Atom.make(0).pipe(Atom.setIdleTTL(5 * 60_000)),
52 +);
53 +
54 export const composerPullRequests = createPullRequestEnvironmentAtoms(connectionAtomRuntime, {
55 // Rows on pages past each listing's head; the head itself is patched where it is cached.
56 actionReply: (target, reply, registry) =>
@@ -69,6 +79,10 @@ export const composerPullRequests = createPullRequestEnvironmentAtoms(connection
79 if (Option.isSome(previous) && previous.value.headSha !== value.headSha)
80 registry.refresh(composerPullRequests.filesViewed(target));
81 }),
82 + filesViewed: (target, _value, registry) =>
83 + Effect.sync(() =>
84 + registry.update(filesViewedReadsFamily(filesViewedReadsKey(target)), (reads) => reads + 1),
85 + ),
86 list: (target, value, registry) =>
87 Effect.sync(() => {
88 const statsAtom = pullRequestListStatsFamily(target.environmentId);
apps/server/src/pullRequest/PullRequestService.test.ts
+37
@@ -4419,6 +4419,43 @@ it.effect("keeps viewed files itself for a host that keeps none of its own", ()
4419 }),
4420 );
4421
4422 +it.effect("records shown revisions until the reader ticks the pushed version", () =>
4423 + Effect.gen(function* () {
4424 + const asked: Array<ReadonlyArray<string>> = [];
4425 + const revisions = new Map([
4426 + ["src/a.ts", "pushed-a"],
4427 + ["src/b.ts", "current-b"],
4428 + ]);
4429 + const service = yield* environmentViewedService(revisions, asked);
4430 +
4431 + yield* service.setFilesViewed({
4432 + ...GITLAB_REFERENCE,
4433 + files: [
4434 + { path: "src/a.ts", viewed: true, revision: "shown-a" },
4435 + { path: "src/b.ts", viewed: true },
4436 + ],
4437 + });
4438 + assert.deepStrictEqual(asked, [["src/b.ts"]]);
4439 + yield* service.invalidate({ reference: GITLAB_REFERENCE });
4440 + assert.deepStrictEqual((yield* service.filesViewed(GITLAB_REFERENCE)).files, [
4441 + { path: "src/a.ts", state: "dismissed" },
4442 + { path: "src/b.ts", state: "viewed" },
4443 + ]);
4444 +
4445 + const reads = asked.length;
4446 + revisions.set("src/a.ts", "pushed-a-again");
4447 + yield* service.setFilesViewed({
4448 + ...GITLAB_REFERENCE,
4449 + files: [{ path: "src/a.ts", viewed: true, revision: "pushed-a-again" }],
4450 + });
4451 + assert.strictEqual(asked.length, reads);
4452 + assert.deepStrictEqual((yield* service.filesViewed(GITLAB_REFERENCE)).files, [
4453 + { path: "src/a.ts", state: "viewed" },
4454 + { path: "src/b.ts", state: "viewed" },
4455 + ]);
4456 + }),
4457 +);
4458 +
4459 it.effect("keeps a deleted file cleared, which the head has no version of at all", () =>
4460 Effect.gen(function* () {
4461 const service = yield* environmentViewedService(new Map(), []);
apps/server/src/pullRequest/PullRequestService.ts
+11 -2
@@ -2085,7 +2085,14 @@ export const make = Effect.gen(function* () {
2085 // Deliberately not `invalidatedByMutation`: ticking a file off says nothing about the
2086 // change request, and dropping a 300-file diff on every checkbox is the whole cost of
2087 // the feature.
2088 - Effect.ensuring(Effect.sync(() => bumpFilesViewedEpoch(ref))),
2088 + Effect.ensuring(
2089 + Effect.sync(() => {
2090 + bumpFilesViewedEpoch(ref);
2091 + if (input.files.some((file) => file.viewed && file.revision !== undefined)) {
2092 + bumpEpoch(fileRevisionEpochs, ref);
2093 + }
2094 + }),
2095 + ),
2096 ),
2097 ),
2098 );
@@ -3001,6 +3008,7 @@ export const make = Effect.gen(function* () {
3008 const filesViewedEpoch = (ref: PullRequestRef) => filesViewedEpochs.get(refScope(ref)) ?? 0;
3009 const bumpFilesViewedEpoch = (ref: PullRequestRef) => bumpEpoch(filesViewedEpochs, ref);
3010
3011 + const fileRevisionEpochs = new Map<string, number>();
3012 let everyFileRevisionEpoch = 0;
3013 const viewedFiles = ViewedFiles.make({
3014 filesViewedStore,
@@ -3009,7 +3017,8 @@ export const make = Effect.gen(function* () {
3017 toPullRequestError,
3018 runFork,
3019 refEpoch,
3012 - fileRevisionsEpoch: () => everyFileRevisionEpoch,
3020 + fileRevisionsEpoch: (ref) =>
3021 + `${everyFileRevisionEpoch}:${fileRevisionEpochs.get(refScope(ref)) ?? 0}`,
3022 });
3023
3024 const filtersOfKey = (
apps/server/src/pullRequest/pullRequestViewedFiles.ts
+2 -2
@@ -30,7 +30,7 @@ const MAX_FILE_REVISION_PATHS = 1_000;
30 interface FileRevisionsDependencies {
31 readonly runFork: (effect: Effect.Effect<void>) => unknown;
32 readonly refEpoch: (ref: PullRequestRef) => number;
33 - readonly fileRevisionsEpoch: () => number;
33 + readonly fileRevisionsEpoch: (ref: PullRequestRef) => string;
34 readonly toPullRequestError: (
35 operation: string,
36 ) => (error: PullRequestProviderError) => PullRequestError;
@@ -50,7 +50,7 @@ const makeFileRevisions = (dependencies: FileRevisionsDependencies) => {
50 const fileRevisionsKey = (project: SupportedProject, ref: PullRequestRef) =>
51 [
52 refEpoch({ ...ref, host: project.host, repository: project.repository }),
53 - fileRevisionsEpoch(),
53 + fileRevisionsEpoch({ ...ref, host: project.host, repository: project.repository }),
54 ref.projectId,
55 project.repository.trim().toLowerCase(),
56 ref.number,
apps/web/src/components/pullRequest/filesViewedStore.test.ts
+3 -8
@@ -71,7 +71,8 @@ function setup(initial: PullRequestFilesViewedResult | null = answer(false)) {
71
72 describe("createFilesViewedStore", () => {
73 it("batches presses and holds them until a read that could have seen them", async () => {
74 - using t = setup();
74 + const unchanged = { files: [{ path: "a.ts", state: "dismissed" as const }], truncated: false };
75 + using t = setup(unchanged);
76 const scope = t.store.scope(first);
77 const release = scope.subscribe(() => {});
78 scope.setViewed("a.ts", true, "shown-a");
@@ -88,10 +89,7 @@ describe("createFilesViewedStore", () => {
89 expect(scope.getSnapshot().get("a.ts")).toBe(true);
90
91 // A push landed between the write and the next read: the host's answer wins.
91 - t.answerFor(first, {
92 - files: [{ path: "a.ts", state: "dismissed" }],
93 - truncated: false,
94 - });
92 + t.answerFor(first, unchanged);
93 expect(scope.getSnapshot().size).toBe(0);
94 release();
95 });
@@ -205,9 +203,6 @@ describe("createFilesViewedStore", () => {
203 await vi.advanceTimersByTimeAsync(400);
204 t.settles[0]?.(AsyncResult.success(undefined));
205 await vi.advanceTimersByTimeAsync(0);
208 - // The same answer published again is not a new read.
209 - t.answerFor(first, agreeing);
210 - expect(scope.getSnapshot().get("a.ts")).toBe(true);
206 t.answerFor(first, answer(false));
207 expect(scope.getSnapshot().size).toBe(0);
208 release();
apps/web/src/components/pullRequest/filesViewedStore.ts
+15 -26
@@ -8,10 +8,12 @@ import type {
8 PullRequestFilesViewedResult,
9 PullRequestRef,
10 } from "@t3tools/contracts";
11 -import { AsyncResult } from "effect/reactivity";
11
12 import { appAtomRegistry } from "~/rpc/atomRegistry";
14 -import { pullRequestEnvironment } from "~/state/pullRequests";
13 +import {
14 + pullRequestEnvironment,
15 + subscribePullRequestFilesViewedAnswers,
16 +} from "~/state/pullRequests";
17
18 import { toastManager } from "../ui/toast";
19 import {
@@ -36,7 +38,7 @@ type FilesViewedBatch = ReadonlyArray<{
38
39 export interface FilesViewedStoreDeps {
40 readonly flushDelayMs: number;
39 - /** Calls `onAnswer` with the host's current answer at once, then with every later one. */
41 + /** Calls `onAnswer` only after completed server reads, excluding persisted values. */
42 readonly subscribeAnswers: (
43 target: FilesViewedTarget,
44 onAnswer: (answer: PullRequestFilesViewedResult | null) => void,
@@ -68,13 +70,8 @@ interface FilesViewedScope {
70 readonly sentBy: Map<string, number>;
71 requests: number;
72 timer: ReturnType<typeof setTimeout> | null;
71 - answer: PullRequestFilesViewedResult | null;
72 - /**
73 - * The host's answer as it stood when a completed write was acknowledged, per path. The first
74 - * answer that differs from it is the first read that could have seen the write, which is what
75 - * retires the press rather than the host happening to agree with it.
76 - */
77 - readonly answeredFrom: Map<string, PullRequestFilesViewedResult | null>;
73 + // Completed reads retire acknowledged presses even when equal data is structurally shared.
74 + readonly answeredFrom: Set<string>;
75 }
76
77 /**
@@ -95,12 +92,11 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
92 const isLive = (key: string, scope: FilesViewedScope) => scopes.get(key) === scope;
93
94 const settle = (scope: FilesViewedScope, answer: PullRequestFilesViewedResult | null) => {
98 - if (answer === scope.answer) return;
99 - scope.answer = answer;
95 + if (answer === null) return;
96 const pending = new Set([...scope.queued.keys(), ...scope.sentBy.keys()]);
97 const answered = new Set<string>();
102 - for (const [path, from] of scope.answeredFrom) {
103 - if (pending.has(path) || from === answer) continue;
98 + for (const path of scope.answeredFrom) {
99 + if (pending.has(path)) continue;
100 answered.add(path);
101 scope.answeredFrom.delete(path);
102 }
@@ -141,7 +137,7 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
137 }
138 // Answered for from the next read on, whatever it says. A push landing between the write
139 // and that read comes back as `dismissed`, and the press must not stand over it.
144 - for (const path of mine) scope.answeredFrom.set(path, scope.answer);
140 + for (const path of mine) scope.answeredFrom.add(path);
141 deps.refresh(scope.target);
142 });
143 };
@@ -160,10 +156,8 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
156 if (scope === undefined) {
157 const listeners = new Set<() => void>();
158 let created: FilesViewedScope | undefined;
163 - let initial: PullRequestFilesViewedResult | null = null;
159 const unsubscribeAnswers = deps.subscribeAnswers(target, (answer) => {
165 - if (created === undefined) initial = answer;
166 - else settle(created, answer);
160 + if (created !== undefined) settle(created, answer);
161 });
162 created = {
163 target,
@@ -174,8 +168,7 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
168 sentBy: new Map(),
169 requests: 0,
170 timer: null,
177 - answer: initial,
178 - answeredFrom: new Map(),
171 + answeredFrom: new Set(),
172 };
173 scope = created;
174 scopes.set(key, scope);
@@ -240,12 +233,8 @@ const filesViewedAtom = ({ environmentId, reference }: FilesViewedTarget) =>
233
234 const filesViewedStore = createFilesViewedStore({
235 flushDelayMs: 400,
243 - subscribeAnswers: (target, onAnswer) =>
244 - appAtomRegistry.subscribe(
245 - filesViewedAtom(target),
246 - (result) => onAnswer(AsyncResult.getOrElse(result, () => null)),
247 - { immediate: true },
248 - ),
236 + subscribeAnswers: ({ environmentId, reference }, onAnswer) =>
237 + subscribePullRequestFilesViewedAnswers({ environmentId, input: reference }, onAnswer),
238 send: ({ environmentId, reference }, files) =>
239 runAtomCommand(
240 appAtomRegistry,
apps/web/src/state/pullRequests.ts
+37
@@ -12,6 +12,7 @@ import {
12 type EnvironmentId,
13 type ProjectId,
14 type PullRequestChecksState,
15 + type PullRequestFilesViewedResult,
16 type PullRequestListInput,
17 type PullRequestFilterOptionsInput,
18 type PullRequestListStatsInput,
@@ -369,8 +370,44 @@ export function restoredPullRequestList(
370 return restorePullRequestList(snapshotStorage(), targets);
371 }
372
373 +const filesViewedAnswerListeners = new Map<
374 + string,
375 + Set<(answer: PullRequestFilesViewedResult) => void>
376 +>();
377 +
378 +function filesViewedAnswerKey({ environmentId, input }: EnvironmentQueryTarget<PullRequestRef>) {
379 + return JSON.stringify([
380 + environmentId,
381 + input.projectId,
382 + input.host,
383 + input.expectedAccountId,
384 + input.allowStale,
385 + input.repository,
386 + input.number,
387 + ]);
388 +}
389 +
390 +export function subscribePullRequestFilesViewedAnswers(
391 + target: EnvironmentQueryTarget<PullRequestRef>,
392 + onAnswer: (answer: PullRequestFilesViewedResult) => void,
393 +) {
394 + const key = filesViewedAnswerKey(target);
395 + let listeners = filesViewedAnswerListeners.get(key);
396 + if (listeners === undefined) filesViewedAnswerListeners.set(key, (listeners = new Set()));
397 + listeners.add(onAnswer);
398 + return () => {
399 + listeners.delete(onAnswer);
400 + if (listeners.size === 0) filesViewedAnswerListeners.delete(key);
401 + };
402 +}
403 +
404 // Answers are stamped on arrival: older servers leave no other ordering for same-dated reads.
405 export const pullRequestEnvironment = createPullRequestEnvironmentAtoms(connectionAtomRuntime, {
406 + filesViewed: (target, answer) =>
407 + Effect.sync(() => {
408 + for (const listener of filesViewedAnswerListeners.get(filesViewedAnswerKey(target)) ?? [])
409 + listener(answer);
410 + }),
411 list: ({ environmentId, input }, answer, registry) =>
412 Effect.map(Clock.currentTimeMillis, (observedAt) => {
413 rememberPullRequestListAnswer(snapshotStorage(), { environmentId, input }, answer);
packages/client-runtime/src/state/pullRequests.ts
+2
@@ -390,6 +390,7 @@ export interface PullRequestAnswerHandlers {
390 readonly list?: QueryAnswerHandler<PullRequestListInput, PullRequestListResult>;
391 readonly listStats?: QueryAnswerHandler<PullRequestListStatsInput, PullRequestListStatsResult>;
392 readonly detail?: QueryAnswerHandler<PullRequestRef, PullRequestDetail>;
393 + readonly filesViewed?: QueryAnswerHandler<PullRequestRef, PullRequestFilesViewedResult>;
394 readonly checks?: QueryAnswerHandler<PullRequestRef, PullRequestChecks | null>;
395 readonly reviewGuide?: QueryAnswerHandler<PullRequestRef, PullRequestReviewGuideResult>;
396 /** Once per action reply, after the loaded readers show it; unscoped references only. */
@@ -839,6 +840,7 @@ export function createPullRequestEnvironmentAtoms<R, E>(
840 }),
841 filesViewed: createEnvironmentRpcQueryAtomFamily(runtime, {
842 label: "environment-data:pull-requests:files-viewed",
843 + ...(onAnswer.filesViewed === undefined ? {} : { onAnswer: onAnswer.filesViewed }),
844 persistence: persistQuery(PullRequestFilesViewedResult),
845 tag: WS_METHODS.pullRequestsFilesViewed,
846 execute: (input) => routedRequest(WS_METHODS.pullRequestsFilesViewed, input),