TypeScript
fix(client): a resume no longer re-renders every home row five times (#27404)
ahalekellybd907328f38a
diff --git a/apps/mobile/src/state/session.test.ts b/apps/mobile/src/state/session.test.tsindex c965f8d26bf..b47054e2586 100644--- a/apps/mobile/src/state/session.test.ts+++ b/apps/mobile/src/state/session.test.ts@@ -13,9 +13,10 @@ vi.mock("react", () => ({ useMemo: <A>(factory: () => A) => factory() }));vi.mock("@effect/atom-react", async () => {const { appAtomRegistry } = await import("./atom-registry");return {- useAtomValue: <A>(atom: Atom.Atom<A>) => {+ useAtomValue: <A, B>(atom: Atom.Atom<A>, select?: (value: A) => B) => {harness.lastAtom = atom;- return appAtomRegistry.get(atom);+ const value = appAtomRegistry.get(atom);+ return select === undefined ? value : select(value);},};});diff --git a/apps/mobile/src/state/session.ts b/apps/mobile/src/state/session.tsindex c7fbecab580..5d6a441e9b9 100644--- a/apps/mobile/src/state/session.ts+++ b/apps/mobile/src/state/session.ts@@ -29,12 +29,13 @@ export function useEnvironmentScope(environmentId: EnvironmentId | null,scope: AuthEnvironmentScope,): boolean {- const result = useAtomValue(+ // Select the grant: every revalidation emits a new result, and most consumers are list rows.+ return useAtomValue(environmentId === null? EMPTY_SESSION_STATE_ATOM: environmentSession.sessionStateAtom(environmentId),+ (result) => sessionHasScope(result, scope),);- return sessionHasScope(result, scope);}export function useEnvironmentsWithScope(diff --git a/apps/web/src/components/ServerUpdateAction.tsx b/apps/web/src/components/ServerUpdateAction.tsxindex 1d82a7628a2..32a2811aca1 100644--- a/apps/web/src/components/ServerUpdateAction.tsx+++ b/apps/web/src/components/ServerUpdateAction.tsx@@ -272,7 +272,7 @@ export function ServerUpdateAction({const isDesktopAppUpdate = selfUpdate === "desktop-managed";const serverVersion = useServerConfigs().get(environmentId)?.environment.serverVersion;const sessionStateAtom = environmentSession.sessionStateAtom(environmentId);- const canUpdate = canUpdateServer(useAtomValue(sessionStateAtom));+ const canUpdate = useAtomValue(sessionStateAtom, canUpdateServer);const updatesDenial = managedUpdatesDenial(useManagedPolicy(environmentId));const update = useServerUpdate();const { copyToClipboard } = useCopyToClipboard<{ command: string }>({diff --git a/apps/web/src/state/session.ts b/apps/web/src/state/session.tsindex 13dffb510b9..00c38aec3df 100644--- a/apps/web/src/state/session.ts+++ b/apps/web/src/state/session.ts@@ -25,12 +25,13 @@ export function useEnvironmentScope(environmentId: EnvironmentId | null,scope: AuthEnvironmentScope,): boolean {- const result = useAtomValue(+ // Select the grant: every revalidation emits a new result, and most consumers are list rows.+ return useAtomValue(environmentId === null? EMPTY_SESSION_STATE_ATOM: environmentSession.sessionStateAtom(environmentId),+ (result) => sessionHasScope(result, scope),);- return sessionHasScope(result, scope);}export function sessionHasScope(diff --git a/packages/client-runtime/src/state/shellReducer.test.ts b/packages/client-runtime/src/state/shellReducer.test.tsindex 3fbd9b1b8d3..f98a14c3ed3 100644--- a/packages/client-runtime/src/state/shellReducer.test.ts+++ b/packages/client-runtime/src/state/shellReducer.test.ts@@ -87,22 +87,31 @@ describe("applyShellStreamEvent", () => {});it("authoritative lower-sequence reset replaces client-ahead structural state", () => {+ const unchanged = { ...v2Project, id: ProjectId.make("project-unchanged"), repositoryIdentity };const previous = {...v2ShellSnapshot,snapshotSequence: 9,- projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],+ projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }, unchanged],threads: [{ ...v2ThreadShell, title: "Local-only thread" }],};const next = mergeShellSnapshotProjects(previous, {...v2ShellSnapshot,snapshotSequence: 3,- projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],+ projects: [+ { ...v2Project, title: "Server reset", repositoryIdentity: null },+ { ...unchanged, scripts: [...unchanged.scripts] },+ ],threads: [{ ...v2ThreadShell, title: "Server thread" }],});expect(next.snapshotSequence).toBe(3);expect(next.projects[0]?.title).toBe("Server reset");expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);+ expect(next.projects[1]).toBe(unchanged);+ expect(+ mergeShellSnapshotProjects(next, { ...v2ShellSnapshot, projects: [...next.projects] })+ .projects,+ ).toBe(next.projects);expect(next.threads[0]?.title).toBe("Server thread");});diff --git a/packages/client-runtime/src/state/shellReducer.ts b/packages/client-runtime/src/state/shellReducer.tsindex bf8f7141b31..84911439969 100644--- a/packages/client-runtime/src/state/shellReducer.ts+++ b/packages/client-runtime/src/state/shellReducer.ts@@ -1,14 +1,15 @@import {+ OrchestrationProjectShell,OrchestrationV2ThreadShell,RepositoryIdentity,ThreadPullRequestLink,- type OrchestrationProjectShell,type OrchestrationV2ShellSnapshot,type OrchestrationV2ShellStreamItem,} from "@t3tools/contracts";import * as Schema from "effect/Schema";const sameThreadShell = Schema.toEquivalence(OrchestrationV2ThreadShell);+const sameProjectShell = Schema.toEquivalence(OrchestrationProjectShell);const sameLinks = Schema.toEquivalence(Schema.UndefinedOr(Schema.Array(ThreadPullRequestLink)));const sameRepositoryIdentity = Schema.toEquivalence(Schema.UndefinedOr(Schema.NullOr(RepositoryIdentity)),@@ -116,16 +117,16 @@ export function mergeShellSnapshotProjects(}const previousById = new Map(previous.projects.map((project) => [project.id, project] as const));- return {- ...next,- projects: next.projects.map((project) => {- const prior = previousById.get(project.id);- if (resolvedRootSet?.has(project.workspaceRoot) === true) {- return project;- }- return retainRepositoryIdentity(prior, project);- }),- };+ // Project atoms and shell persistence compare by reference, so unchanged projects keep their objects.+ const projects = next.projects.map((project) => {+ const prior = previousById.get(project.id);+ const merged = retainRepositoryIdentity(prior, project);+ return prior !== undefined && sameProjectShell(prior, merged) ? prior : merged;+ });+ const unchanged =+ projects.length === previous.projects.length &&+ projects.every((project, index) => project === previous.projects[index]);+ return { ...next, projects: unchanged ? previous.projects : projects };}export function applyShellStreamEvent(