Skip to content
TypeScript

fix(client): retain PR links while reconnect metadata changes (#17365)

Maxwell Youngf1003567108e
diff --git a/packages/client-runtime/src/state/shellReducer.test.ts b/packages/client-runtime/src/state/shellReducer.test.ts
index 6f09385da2b..ffbe173ef1b 100644
--- a/packages/client-runtime/src/state/shellReducer.test.ts
+++ b/packages/client-runtime/src/state/shellReducer.test.ts
@@ -516,4 +516,49 @@ describe("reuseUnchangedThreadShells", () => {
incoming,
);
});
+
+ it("retains deferred links when the only row changes status", () => {
+ const { pullRequests: _links, ...withoutLinks } = linked;
+ const next = reuseUnchangedThreadShells(
+ { ...v2ShellSnapshot, threads: [linked] },
+ {
+ ...v2ShellSnapshot,
+ snapshotSequence: 5,
+ threads: [{ ...withoutLinks, status: "running" }],
+ },
+ );
+ expect(next.snapshotSequence).toBe(5);
+ expect(next.threads[0]?.status).toBe("running");
+ expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
+ });
+
+ it("retains deferred links on a renamed row alongside an unchanged sibling", () => {
+ const { pullRequests: _links, ...withoutLinks } = linked;
+ const next = reuseUnchangedThreadShells(
+ { ...v2ShellSnapshot, threads: [linked, other] },
+ { ...v2ShellSnapshot, threads: [{ ...withoutLinks, title: "Renamed" }, { ...other }] },
+ );
+ expect(next.threads[0]?.title).toBe("Renamed");
+ expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
+ expect(next.threads[1]).toBe(other);
+ });
+
+ it("keeps an explicit link removal when metadata also changes", () => {
+ const updated = { ...linked, title: "Renamed", pullRequests: [] };
+ const incoming = { ...v2ShellSnapshot, threads: [updated] };
+ const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
+ expect(next).toBe(incoming);
+ expect(next.threads[0]?.pullRequests).toEqual([]);
+ });
+
+ it("keeps explicitly refreshed links instead of the previous links", () => {
+ const updated = {
+ ...linked,
+ pullRequests: [{ ...link, number: 4, url: "https://github.com/pingdotgg/t3code/pull/4" }],
+ };
+ const incoming = { ...v2ShellSnapshot, threads: [updated] };
+ const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
+ expect(next).toBe(incoming);
+ expect(next.threads[0]?.pullRequests).toBe(updated.pullRequests);
+ });
});
diff --git a/packages/client-runtime/src/state/shellReducer.ts b/packages/client-runtime/src/state/shellReducer.ts
index b37bc38a1af..eec7f6c949f 100644
--- a/packages/client-runtime/src/state/shellReducer.ts
+++ b/packages/client-runtime/src/state/shellReducer.ts
@@ -31,7 +31,7 @@ export function reuseUnchangedThreadShells(
): OrchestrationV2ShellSnapshot {
if (previous === null || previous.threads.length === 0) return next;
const previousById = new Map(previous.threads.map((thread) => [thread.id, thread] as const));
- let reused = 0;
+ let changed = false;
const threads = next.threads.map((thread) => {
const prior = previousById.get(thread.id);
if (prior === undefined) return thread;
@@ -39,11 +39,11 @@ export function reuseUnchangedThreadShells(
thread.pullRequests === undefined && prior.pullRequests !== undefined
? { ...thread, pullRequests: prior.pullRequests }
: thread;
- if (!sameThreadShell(prior, candidate)) return thread;
- reused += 1;
- return prior;
+ const retained = sameThreadShell(prior, candidate) ? prior : candidate;
+ if (retained !== thread) changed = true;
+ return retained;
});
- return reused === 0 ? next : { ...next, threads };
+ return changed ? { ...next, threads } : next;
}
function upsertById<T extends { readonly id: unknown }>(