Skip to content

Commit 9e074bc

Browse files
perf(web): another thread's shell push no longer re-renders the open thread view
ThreadRouteView, ChatView and DesktopAppActivationCoordinator read the whole environment shell state, which every shell push replaces, to learn only its status or whether a snapshot exists. They now read per-environment status and has-snapshot atoms that keep their primitive value through thread pushes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fa7c611 commit 9e074bc

7 files changed

Lines changed: 97 additions & 19 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ import {
381381
useThreadCheckpointSummaries,
382382
waitForThreadShell,
383383
} from "../state/entities";
384-
import { environmentShell } from "../state/shell";
384+
import { useEnvironmentShellHasSnapshot } from "../state/shell";
385385
import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";
386386
import { createPageScrollController, type PageScrollKey } from "./chat/pageScrollController";
387387
import { isTimelineScrollTarget } from "./chat/timelineScrollTarget";
@@ -1889,10 +1889,9 @@ export default function ChatView(props: ChatViewProps) {
18891889
});
18901890
};
18911891
const handleOpenDraftProjectSettingsStable = useStableCallback(handleOpenDraftProjectSettings);
1892-
const activeEnvironmentShell = useEnvironmentQuery(
1893-
activeThread ? environmentShell.stateAtom(activeThread.environmentId) : null,
1892+
const activeEnvironmentBootstrapComplete = useEnvironmentShellHasSnapshot(
1893+
activeThread?.environmentId ?? null,
18941894
);
1895-
const activeEnvironmentBootstrapComplete = activeEnvironmentShell.data?.snapshot._tag === "Some";
18961895
const activeProjectKey = activeProject
18971896
? `${activeProject.environmentId}:${activeProject.workspaceRoot}`
18981897
: null;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
} from "../composerDraftStore";
1616
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
1717
import { useEnvironmentThreadRefs, useThreadRefs, useThreadShell } from "../state/entities";
18-
import { useEnvironmentQuery } from "../state/query";
19-
import { environmentShell } from "../state/shell";
18+
import { useEnvironmentShellStatus } from "../state/shell";
2019
import {
2120
buildThreadRouteParams,
2221
isThreadRouteSnapshotAuthoritative,
@@ -58,12 +57,10 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
5857
})
5958
: null;
6059

61-
const shell = useEnvironmentQuery(
62-
serverThreadRef === null ? null : environmentShell.stateAtom(serverThreadRef.environmentId),
63-
);
60+
const shellStatus = useEnvironmentShellStatus(serverThreadRef?.environmentId ?? null);
6461
const serverThreadShell = serverThread;
6562
const environmentThreadRefs = useEnvironmentThreadRefs(serverThreadRef?.environmentId ?? null);
66-
const bootstrapComplete = isThreadRouteSnapshotAuthoritative(shell.data?.status);
63+
const bootstrapComplete = isThreadRouteSnapshotAuthoritative(shellStatus);
6764
const draftThread = useComposerDraftStore((store) =>
6865
serverThreadRef ? store.getDraftThreadByRef(serverThreadRef) : null,
6966
);

‎apps/web/src/components/desktop/DesktopAppActivationCoordinator.tsx‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { newProjectId } from "../../lib/utils";
99
import { readProjects, waitForProject } from "../../state/entities";
1010
import { usePrimaryEnvironment } from "../../state/environments";
1111
import { projectEnvironment } from "../../state/projects";
12-
import { useEnvironmentQuery } from "../../state/query";
13-
import { environmentShell } from "../../state/shell";
12+
import { useEnvironmentShellHasSnapshot } from "../../state/shell";
1413
import { useAtomCommand } from "../../state/use-atom-command";
1514

1615
export function DesktopAppActivationCoordinator() {
@@ -19,16 +18,14 @@ export function DesktopAppActivationCoordinator() {
1918
const openThread = useNewThreadHandler();
2019
const queueRef = useRef(Promise.resolve());
2120
const activation = window.desktopBridge?.appActivation;
22-
const shell = useEnvironmentQuery(
23-
primaryEnvironment === null
24-
? null
25-
: environmentShell.stateAtom(primaryEnvironment.environmentId),
21+
const shellHasSnapshot = useEnvironmentShellHasSnapshot(
22+
primaryEnvironment?.environmentId ?? null,
2623
);
2724
const ready =
2825
activation !== undefined &&
2926
primaryEnvironment?.connection.phase === "connected" &&
3027
primaryEnvironment.serverConfig !== null &&
31-
shell.data?.snapshot._tag === "Some";
28+
shellHasSnapshot;
3229

3330
const processRequest = useEffectEvent(async (request: DesktopAppActivationRequest) =>
3431
handleDesktopAppActivationRequest(request, {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
createEnvironmentSnapshotAtom,
88
createShellEnvironmentAtoms,
99
type EnvironmentShellState,
10+
type EnvironmentShellStatus,
1011
} from "@t3tools/client-runtime/state/shell";
1112
import {
1213
type EnvironmentCatalogState,
1314
enabledEnvironmentIds,
1415
} from "@t3tools/client-runtime/state/connections";
1516
import type { EnvironmentId } from "@t3tools/contracts";
17+
import { useAtomValue } from "@effect/atom-react";
1618
import * as Option from "effect/Option";
1719
import { AsyncResult, Atom } from "effect/reactivity";
1820

@@ -24,6 +26,29 @@ export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntim
2426
export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
2527
export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
2628

29+
const NO_SHELL_STATUS_ATOM = Atom.make<EnvironmentShellStatus | null>(null).pipe(
30+
Atom.withLabel("web-environment-shell-status:none"),
31+
);
32+
const NO_SHELL_SNAPSHOT_ATOM = Atom.make(false).pipe(
33+
Atom.withLabel("web-environment-shell-has-snapshot:none"),
34+
);
35+
36+
export function useEnvironmentShellStatus(
37+
environmentId: EnvironmentId | null,
38+
): EnvironmentShellStatus | null {
39+
return useAtomValue(
40+
environmentId === null ? NO_SHELL_STATUS_ATOM : environmentShell.statusAtom(environmentId),
41+
);
42+
}
43+
44+
export function useEnvironmentShellHasSnapshot(environmentId: EnvironmentId | null): boolean {
45+
return useAtomValue(
46+
environmentId === null
47+
? NO_SHELL_SNAPSHOT_ATOM
48+
: environmentShell.hasSnapshotAtom(environmentId),
49+
);
50+
}
51+
2752
export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
2853
const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
2954
if (Option.isNone(catalog)) {

‎apps/web/src/threadRoutes.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type DraftThreadRouteState = {
2222
export type ThreadRouteRenderState = "loading" | "ready" | "missing";
2323

2424
export function isThreadRouteSnapshotAuthoritative(
25-
status: EnvironmentShellStatus | undefined,
25+
status: EnvironmentShellStatus | null | undefined,
2626
): boolean {
2727
return status === "live";
2828
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { Atom, AtomRegistry } from "effect/reactivity";
77

88
import { PrimaryConnectionTarget } from "../connection/model.ts";
99
import type { EnvironmentShellState } from "./shell.ts";
10-
import { createEnvironmentServerConfigsAtom, createEnvironmentShellSummaryAtom } from "./shell.ts";
10+
import {
11+
createEnvironmentServerConfigsAtom,
12+
createEnvironmentShellProgressAtoms,
13+
createEnvironmentShellSummaryAtom,
14+
} from "./shell.ts";
1115
import { v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
1216

1317
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
@@ -231,4 +235,35 @@ describe("environment shell projections", () => {
231235
harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
232236
expect(harness.registry.get(harness.serverConfigsAtom)).toBe(withConfig);
233237
});
238+
239+
it("keeps load progress quiet while threads stream and reports real progress", () => {
240+
const stateAtom = Atom.make(
241+
shellState({ status: "cached", updatedAt: "2026-06-01T00:00:00.000Z" }),
242+
);
243+
const registry = AtomRegistry.make();
244+
const { statusAtom, hasSnapshotAtom } = createEnvironmentShellProgressAtoms(() => stateAtom);
245+
let changes = 0;
246+
const unsubscribeStatus = registry.subscribe(statusAtom(ENVIRONMENT_ID), () => changes++);
247+
const unsubscribeSnapshot = registry.subscribe(
248+
hasSnapshotAtom(ENVIRONMENT_ID),
249+
() => changes++,
250+
);
251+
252+
registry.set(
253+
stateAtom,
254+
shellState({ status: "cached", updatedAt: "2026-06-01T00:00:01.000Z" }),
255+
);
256+
expect(changes).toBe(0);
257+
258+
registry.set(stateAtom, shellState({ status: "live", updatedAt: "2026-06-01T00:00:02.000Z" }));
259+
expect(registry.get(statusAtom(ENVIRONMENT_ID))).toBe("live");
260+
expect(changes).toBe(1);
261+
262+
registry.set(stateAtom, shellState({ status: "live" }));
263+
expect(registry.get(hasSnapshotAtom(ENVIRONMENT_ID))).toBe(false);
264+
expect(changes).toBe(2);
265+
266+
unsubscribeStatus();
267+
unsubscribeSnapshot();
268+
});
234269
});

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,27 @@ export function createEnvironmentServerConfigsAtom(input: {
522522
}).pipe(Atom.withLabel("environment-server-configs"));
523523
}
524524

525+
/**
526+
* Every shell push replaces the state, so views that gate on load progress read these
527+
* primitives instead and stay put while threads stream.
528+
*/
529+
export function createEnvironmentShellProgressAtoms(
530+
stateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>,
531+
) {
532+
return {
533+
statusAtom: Atom.family((environmentId: EnvironmentId) =>
534+
Atom.make((get) => get(stateValueAtom(environmentId)).status).pipe(
535+
Atom.withLabel(`environment-shell-status:${environmentId}`),
536+
),
537+
),
538+
hasSnapshotAtom: Atom.family((environmentId: EnvironmentId) =>
539+
Atom.make((get) => Option.isSome(get(stateValueAtom(environmentId)).snapshot)).pipe(
540+
Atom.withLabel(`environment-shell-has-snapshot:${environmentId}`),
541+
),
542+
),
543+
};
544+
}
545+
525546
export function createEnvironmentShellAtoms<R, E>(
526547
runtime: Atom.AtomRuntime<
527548
| EnvironmentRegistry.EnvironmentRegistry
@@ -543,9 +564,13 @@ export function createEnvironmentShellAtoms<R, E>(
543564
).pipe(Atom.withLabel(`environment-shell-state-value:${environmentId}`)),
544565
);
545566

567+
const { statusAtom, hasSnapshotAtom } = createEnvironmentShellProgressAtoms(stateValueAtom);
568+
546569
return {
547570
stateAtom,
548571
stateValueAtom,
572+
statusAtom,
573+
hasSnapshotAtom,
549574
};
550575
}
551576

0 commit comments

Comments
 (0)