Skip to content

Commit bd90732

Browse files
ahalekellyclaudemaria-rcks
authored
fix(client): a resume no longer re-renders every home row five times (#27404)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 9b72088 commit bd90732

6 files changed

Lines changed: 33 additions & 20 deletions

File tree

‎apps/mobile/src/state/session.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ vi.mock("react", () => ({ useMemo: <A>(factory: () => A) => factory() }));
1313
vi.mock("@effect/atom-react", async () => {
1414
const { appAtomRegistry } = await import("./atom-registry");
1515
return {
16-
useAtomValue: <A>(atom: Atom.Atom<A>) => {
16+
useAtomValue: <A, B>(atom: Atom.Atom<A>, select?: (value: A) => B) => {
1717
harness.lastAtom = atom;
18-
return appAtomRegistry.get(atom);
18+
const value = appAtomRegistry.get(atom);
19+
return select === undefined ? value : select(value);
1920
},
2021
};
2122
});

‎apps/mobile/src/state/session.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ export function useEnvironmentScope(
2929
environmentId: EnvironmentId | null,
3030
scope: AuthEnvironmentScope,
3131
): boolean {
32-
const result = useAtomValue(
32+
// Select the grant: every revalidation emits a new result, and most consumers are list rows.
33+
return useAtomValue(
3334
environmentId === null
3435
? EMPTY_SESSION_STATE_ATOM
3536
: environmentSession.sessionStateAtom(environmentId),
37+
(result) => sessionHasScope(result, scope),
3638
);
37-
return sessionHasScope(result, scope);
3839
}
3940

4041
export function useEnvironmentsWithScope(

‎apps/web/src/components/ServerUpdateAction.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export function ServerUpdateAction({
272272
const isDesktopAppUpdate = selfUpdate === "desktop-managed";
273273
const serverVersion = useServerConfigs().get(environmentId)?.environment.serverVersion;
274274
const sessionStateAtom = environmentSession.sessionStateAtom(environmentId);
275-
const canUpdate = canUpdateServer(useAtomValue(sessionStateAtom));
275+
const canUpdate = useAtomValue(sessionStateAtom, canUpdateServer);
276276
const updatesDenial = managedUpdatesDenial(useManagedPolicy(environmentId));
277277
const update = useServerUpdate();
278278
const { copyToClipboard } = useCopyToClipboard<{ command: string }>({

‎apps/web/src/state/session.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ export function useEnvironmentScope(
2525
environmentId: EnvironmentId | null,
2626
scope: AuthEnvironmentScope,
2727
): boolean {
28-
const result = useAtomValue(
28+
// Select the grant: every revalidation emits a new result, and most consumers are list rows.
29+
return useAtomValue(
2930
environmentId === null
3031
? EMPTY_SESSION_STATE_ATOM
3132
: environmentSession.sessionStateAtom(environmentId),
33+
(result) => sessionHasScope(result, scope),
3234
);
33-
return sessionHasScope(result, scope);
3435
}
3536

3637
export function sessionHasScope(

‎packages/client-runtime/src/state/shellReducer.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,31 @@ describe("applyShellStreamEvent", () => {
8787
});
8888

8989
it("authoritative lower-sequence reset replaces client-ahead structural state", () => {
90+
const unchanged = { ...v2Project, id: ProjectId.make("project-unchanged"), repositoryIdentity };
9091
const previous = {
9192
...v2ShellSnapshot,
9293
snapshotSequence: 9,
93-
projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
94+
projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }, unchanged],
9495
threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
9596
};
9697
const next = mergeShellSnapshotProjects(previous, {
9798
...v2ShellSnapshot,
9899
snapshotSequence: 3,
99-
projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
100+
projects: [
101+
{ ...v2Project, title: "Server reset", repositoryIdentity: null },
102+
{ ...unchanged, scripts: [...unchanged.scripts] },
103+
],
100104
threads: [{ ...v2ThreadShell, title: "Server thread" }],
101105
});
102106

103107
expect(next.snapshotSequence).toBe(3);
104108
expect(next.projects[0]?.title).toBe("Server reset");
105109
expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
110+
expect(next.projects[1]).toBe(unchanged);
111+
expect(
112+
mergeShellSnapshotProjects(next, { ...v2ShellSnapshot, projects: [...next.projects] })
113+
.projects,
114+
).toBe(next.projects);
106115
expect(next.threads[0]?.title).toBe("Server thread");
107116
});
108117

‎packages/client-runtime/src/state/shellReducer.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {
2+
OrchestrationProjectShell,
23
OrchestrationV2ThreadShell,
34
RepositoryIdentity,
45
ThreadPullRequestLink,
5-
type OrchestrationProjectShell,
66
type OrchestrationV2ShellSnapshot,
77
type OrchestrationV2ShellStreamItem,
88
} from "@t3tools/contracts";
99
import * as Schema from "effect/Schema";
1010

1111
const sameThreadShell = Schema.toEquivalence(OrchestrationV2ThreadShell);
12+
const sameProjectShell = Schema.toEquivalence(OrchestrationProjectShell);
1213
const sameLinks = Schema.toEquivalence(Schema.UndefinedOr(Schema.Array(ThreadPullRequestLink)));
1314
const sameRepositoryIdentity = Schema.toEquivalence(
1415
Schema.UndefinedOr(Schema.NullOr(RepositoryIdentity)),
@@ -116,16 +117,16 @@ export function mergeShellSnapshotProjects(
116117
}
117118

118119
const previousById = new Map(previous.projects.map((project) => [project.id, project] as const));
119-
return {
120-
...next,
121-
projects: next.projects.map((project) => {
122-
const prior = previousById.get(project.id);
123-
if (resolvedRootSet?.has(project.workspaceRoot) === true) {
124-
return project;
125-
}
126-
return retainRepositoryIdentity(prior, project);
127-
}),
128-
};
120+
// Project atoms and shell persistence compare by reference, so unchanged projects keep their objects.
121+
const projects = next.projects.map((project) => {
122+
const prior = previousById.get(project.id);
123+
const merged = retainRepositoryIdentity(prior, project);
124+
return prior !== undefined && sameProjectShell(prior, merged) ? prior : merged;
125+
});
126+
const unchanged =
127+
projects.length === previous.projects.length &&
128+
projects.every((project, index) => project === previous.projects[index]);
129+
return { ...next, projects: unchanged ? previous.projects : projects };
129130
}
130131

131132
export function applyShellStreamEvent(

0 commit comments

Comments
 (0)