@devries-cameron20 / t3code Mirror · read-only / commits / ce6f8ce36e
Fork 0

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, 13:00 UTC

fix(pr): keep known state in sync across controls (#18408)

maria committed Oct 14, 2026 at 17:45 UTC ce6f8ce36e240d1c615e2130fc017652cbc1ec58
2 files changed +106 -4
apps/web/src/state/pullRequests.test.ts
+36 -1
@@ -2,7 +2,11 @@ import { SourceControlProviderKind } from "@t3tools/contracts";
2 import { ProjectId, type PullRequestSummary } from "@t3tools/contracts";
3 import { describe, expect, it } from "vite-plus/test";
4
5 -import { newestPullRequestObservation, newestPullRequestSummary } from "./pullRequests";
5 +import {
6 + newestPullRequestObservation,
7 + newestPullRequestSummary,
8 + threadPullRequestLinkToSummary,
9 +} from "./pullRequests";
10
11 function summary(overrides: Partial<PullRequestSummary> = {}): PullRequestSummary {
12 return {
@@ -81,4 +85,35 @@ describe("pull request summary cache", () => {
85 expect(newestPullRequestSummary(older, reopened)).toBe(reopened);
86 expect(newestPullRequestSummary(reopened, older)).toBe(reopened);
87 });
88 +
89 + it("reuses saved thread state while detail is stale and lets newer detail replace it", () => {
90 + const stale = summary();
91 + const link = {
92 + host: "github.com",
93 + repository: stale.repository,
94 + number: stale.number,
95 + url: stale.url,
96 + source: "manual" as const,
97 + linkedAt: stale.updatedAt,
98 + stack: null,
99 + snapshot: {
100 + ...stale,
101 + state: "closed" as const,
102 + isDraft: false,
103 + updatedAt: "2026-09-11T00:00:00Z",
104 + syncedAt: "2026-09-12T00:00:00Z",
105 + },
106 + };
107 + const saved = threadPullRequestLinkToSummary(link, stale.projectId);
108 + expect(newestPullRequestSummary(stale, saved)?.state).toBe("closed");
109 + const fresh = summary({ isDraft: true, updatedAt: "2026-09-13T00:00:00Z" });
110 + expect(newestPullRequestSummary(fresh, saved)).toBe(fresh);
111 + expect(threadPullRequestLinkToSummary({ ...link, snapshot: null }, stale.projectId)).toBeNull();
112 + expect(
113 + threadPullRequestLinkToSummary(
114 + { ...link, snapshot: { ...link.snapshot, updatedAt: null } },
115 + stale.projectId,
116 + ),
117 + ).toBeNull();
118 + });
119 });
apps/web/src/state/pullRequests.ts
+70 -3
@@ -1,4 +1,5 @@
1 import { useAtomValue } from "@effect/atom-react";
2 +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3 import {
4 createLinkedPullRequestSummaryAtomFamily,
5 createPullRequestEnvironmentAtoms,
@@ -6,12 +7,18 @@ import {
7 } from "@t3tools/client-runtime/state/pull-requests";
8 import type {
9 EnvironmentId,
10 + ProjectId,
11 PullRequestListInput,
12 PullRequestListStatsInput,
13 PullRequestListEntry,
14 PullRequestRef,
15 PullRequestSummary,
16 + ThreadPullRequestLink,
17 } from "@t3tools/contracts";
18 +import {
19 + threadPullRequestKeyOf,
20 + visibleThreadPullRequests,
21 +} from "@t3tools/shared/threadPullRequests";
22 import * as Option from "effect/Option";
23 import { AsyncResult, Atom } from "effect/reactivity";
24 import { useCallback, useLayoutEffect, useMemo } from "react";
@@ -24,6 +31,7 @@ import {
31 type MergedPullRequestList,
32 } from "../components/pullRequest/pullRequestList.logic";
33 import { formatEnvironmentQueryError } from "./query";
34 +import { environmentThreadShells } from "./threads";
35
36 export const pullRequestEnvironment = createPullRequestEnvironmentAtoms(connectionAtomRuntime);
37 export const linkedPullRequestDetailAtom = createLinkedPullRequestSummaryAtomFamily(
@@ -149,6 +157,58 @@ function observePullRequestSummary(
157 });
158 }
159
160 +export function threadPullRequestLinkToSummary(
161 + link: ThreadPullRequestLink,
162 + projectId: ProjectId,
163 +): PullRequestSummary | null {
164 + // Without the host update time a snapshot cannot be ordered against a fresh read, so it sits out.
165 + const updatedAt = link.snapshot?.updatedAt;
166 + if (link.snapshot === null || updatedAt == null) return null;
167 + return {
168 + ...link.snapshot,
169 + provider: (
170 + sourceControlClients.findByChangeRequestUrl(link.url) ?? sourceControlClients.get(undefined)
171 + ).kind,
172 + projectId,
173 + repository: link.repository,
174 + number: link.number,
175 + url: link.url,
176 + updatedAt,
177 + };
178 +}
179 +
180 +// Saved thread snapshots by environment and host-qualified link, indexed once per shell update
181 +// rather than per control. The host keeps a link to another forge from matching the project's PR.
182 +function savedPullRequestKey(
183 + environmentId: EnvironmentId,
184 + reference: Pick<PullRequestRef, "host" | "repository" | "number">,
185 +): string {
186 + return reference.host === undefined
187 + ? ""
188 + : `${environmentId}:${threadPullRequestKeyOf({ ...reference, host: reference.host })}`;
189 +}
190 +
191 +const savedPullRequestSummariesAtom = Atom.make((get) => {
192 + const summaries = new Map<string, PullRequestSummary>();
193 + for (const thread of get(environmentThreadShells.threadShellsAtom)) {
194 + for (const link of visibleThreadPullRequests(thread.pullRequests)) {
195 + const summary = threadPullRequestLinkToSummary(link, thread.projectId);
196 + if (summary === null) continue;
197 + const key = savedPullRequestKey(thread.environmentId, link);
198 + summaries.set(key, newestPullRequestSummary(summaries.get(key) ?? null, summary) ?? summary);
199 + }
200 + }
201 + return summaries;
202 +});
203 +
204 +const savedPullRequestSummaryAtom = Atom.family((key: string) =>
205 + Atom.make((get) => get(savedPullRequestSummariesAtom).get(key) ?? null).pipe(
206 + // Shell updates also resend unchanged links; keep their subscribers quiet.
207 + Atom.withEquality((previous, next) => JSON.stringify(previous) === JSON.stringify(next)),
208 + Atom.setIdleTTL(5 * 60_000),
209 + ),
210 +);
211 +
212 export function useSharedPullRequestSummary(
213 environmentId: EnvironmentId | null,
214 reference: PullRequestRef | null,
@@ -161,17 +221,24 @@ export function useSharedPullRequestSummary(
221 : pullRequestSummaryKey(environmentId, reference);
222 const atom = observedPullRequestSummaryAtom(key);
223 const observed = useAtomValue(atom);
224 + const saved = useAtomValue(
225 + savedPullRequestSummaryAtom(
226 + environmentId === null || reference === null
227 + ? ""
228 + : savedPullRequestKey(environmentId, reference),
229 + ),
230 + );
231 useLayoutEffect(() => {
232 if (environmentId === null || reference === null || current === null || observedAt === null)
233 return;
234 observePullRequestSummary(environmentId, reference, current, observedAt);
235 }, [current, environmentId, reference, observedAt]);
169 - return (
236 + const summary =
237 newestPullRequestObservation(
238 observed,
239 current === null || observedAt === null ? null : { summary: current, observedAt },
173 - )?.summary ?? current
174 - );
240 + )?.summary ?? current;
241 + return newestPullRequestSummary(summary, saved);
242 }
243 export const pullRequestStackAtom = createPullRequestStackAtomFamily(
244 connectionAtomRuntime,