@devries-cameron20 / t3code Mirror · read-only / commits / edb2155566
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, 05:30 UTC

perf(web): another thread streaming no longer re-renders the app root and thread panels (#20823)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 17, 2026 at 05:02 UTC edb21555667dc926bec3f8b2fac41db817b16ae9
14 files changed +315 -61
apps/web/src/components/ChatView.tsx
+3 -4
@@ -382,7 +382,7 @@ import {
382 useThreadCheckpointSummaries,
383 waitForThreadShell,
384 } from "../state/entities";
385 -import { environmentShell } from "../state/shell";
385 +import { useEnvironmentShellHasSnapshot } from "../state/shell";
386 import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";
387 import { createPageScrollController, type PageScrollKey } from "./chat/pageScrollController";
388 import { isTimelineScrollTarget } from "./chat/timelineScrollTarget";
@@ -1881,10 +1881,9 @@ export default function ChatView(props: ChatViewProps) {
1881 });
1882 };
1883 const handleOpenDraftProjectSettingsStable = useStableCallback(handleOpenDraftProjectSettings);
1884 - const activeEnvironmentShell = useEnvironmentQuery(
1885 - activeThread ? environmentShell.stateAtom(activeThread.environmentId) : null,
1884 + const activeEnvironmentBootstrapComplete = useEnvironmentShellHasSnapshot(
1885 + activeThread?.environmentId ?? null,
1886 );
1887 - const activeEnvironmentBootstrapComplete = activeEnvironmentShell.data?.snapshot._tag === "Some";
1887 const activeProjectKey = activeProject
1888 ? `${activeProject.environmentId}:${activeProject.workspaceRoot}`
1889 : null;
apps/web/src/components/ThreadRouteView.tsx
+3 -6
@@ -15,8 +15,7 @@ import {
15 } from "../composerDraftStore";
16 import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
17 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";
19 import {
20 buildThreadRouteParams,
21 isThreadRouteSnapshotAuthoritative,
@@ -58,12 +57,10 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
57 })
58 : null;
59
61 - const shell = useEnvironmentQuery(
62 - serverThreadRef === null ? null : environmentShell.stateAtom(serverThreadRef.environmentId),
63 - );
60 + const shellStatus = useEnvironmentShellStatus(serverThreadRef?.environmentId ?? null);
61 const serverThreadShell = serverThread;
62 const environmentThreadRefs = useEnvironmentThreadRefs(serverThreadRef?.environmentId ?? null);
66 - const bootstrapComplete = isThreadRouteSnapshotAuthoritative(shell.data?.status);
63 + const bootstrapComplete = isThreadRouteSnapshotAuthoritative(shellStatus);
64 const draftThread = useComposerDraftStore((store) =>
65 serverThreadRef ? store.getDraftThreadByRef(serverThreadRef) : null,
66 );
apps/web/src/components/chat/ThreadRelationshipsControl.agents.test.tsx
+4 -1
@@ -25,7 +25,10 @@ const state = vi.hoisted(() => ({
25 vi.mock("@tanstack/react-router", () => ({ useNavigate: () => state.navigate }));
26 vi.mock("../../state/entities", () => ({
27 useThreadProjection: () => ({ projection: state.projection }),
28 - useThreadShells: () => state.shells,
28 + useThreadRelationshipShells: (ref: { readonly environmentId: string }) =>
29 + (state.shells as ReadonlyArray<{ readonly environmentId: string; readonly source: unknown }>)
30 + .filter((shell) => shell.environmentId === ref.environmentId)
31 + .map((shell) => shell.source),
32 useProjects: () => state.projects,
33 useServerConfigs: () => state.configs,
34 }));
apps/web/src/components/chat/ThreadRelationshipsControl.tsx
+3 -5
@@ -56,7 +56,7 @@ import {
56 useProjects,
57 useServerConfigs,
58 useThreadProjection,
59 - useThreadShells,
59 + useThreadRelationshipShells,
60 } from "../../state/entities";
61 import { threadEnvironment } from "../../state/threads";
62 import { orchestrationEnvironment } from "../../state/orchestration";
@@ -211,7 +211,7 @@ export function ThreadRelationshipsPanel(props: {
211 const ref = scopeThreadRef(props.environmentId, props.threadId);
212 const projection = useThreadProjection(ref)?.projection ?? null;
213 const serverConfigs = useServerConfigs();
214 - const threadShells = useThreadShells();
214 + const relationshipShells = useThreadRelationshipShells(ref);
215 const allProjects = useProjects();
216 const archived = useArchivedThreadSnapshots([props.environmentId]);
217 const navigate = useNavigate();
@@ -257,9 +257,7 @@ export function ThreadRelationshipsPanel(props: {
257 deriveWorkflowGroups(runtimeSubagents).map((group) => [group.workflow.id, group]),
258 );
259 const shells: ReadonlyArray<OrchestrationV2ThreadShell> = [
260 - ...threadShells
261 - .filter((thread) => thread.environmentId === props.environmentId)
262 - .map((thread) => thread.source),
260 + ...relationshipShells,
261 ...(archivedShells ?? []),
262 ];
263 const graph = deriveThreadRelationshipGraph({ threads: shells, projection });
apps/web/src/components/desktop/DesktopAppActivationCoordinator.tsx
+4 -7
@@ -9,8 +9,7 @@ import { newProjectId } from "../../lib/utils";
9 import { readProjects, waitForProject } from "../../state/entities";
10 import { usePrimaryEnvironment } from "../../state/environments";
11 import { projectEnvironment } from "../../state/projects";
12 -import { useEnvironmentQuery } from "../../state/query";
13 -import { environmentShell } from "../../state/shell";
12 +import { useEnvironmentShellHasSnapshot } from "../../state/shell";
13 import { useAtomCommand } from "../../state/use-atom-command";
14
15 export function DesktopAppActivationCoordinator() {
@@ -19,16 +18,14 @@ export function DesktopAppActivationCoordinator() {
18 const openThread = useNewThreadHandler();
19 const queueRef = useRef(Promise.resolve());
20 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,
23 );
24 const ready =
25 activation !== undefined &&
26 primaryEnvironment?.connection.phase === "connected" &&
27 primaryEnvironment.serverConfig !== null &&
31 - shell.data?.snapshot._tag === "Some";
28 + shellHasSnapshot;
29
30 const processRequest = useEffectEvent(async (request: DesktopAppActivationRequest) =>
31 handleDesktopAppActivationRequest(request, {
apps/web/src/components/onboarding/FirstRunGate.tsx
+28 -17
@@ -19,11 +19,7 @@ import {
19 transitionFirstRunGateState,
20 type FirstRunGateState,
21 } from "../../onboarding/firstRun.logic";
22 -import {
23 - useAllEnvironmentShellsBootstrapped,
24 - useProjects,
25 - useThreadShells,
26 -} from "../../state/entities";
22 +import { useAllEnvironmentShellsBootstrapped, useProjects } from "../../state/entities";
23 import { useEnvironments } from "../../state/environments";
24 import { environmentProjects } from "../../state/projects";
25 import { primaryServerConfigAtom, primaryServerWelcomeAtom } from "../../state/server";
@@ -61,6 +57,18 @@ const workspaceEvidenceLiveAtom = Atom.make((get) => {
57 return true;
58 }).pipe(Atom.withLabel("web-onboarding-workspace-evidence-live"));
59
60 +// The gate only needs how many threads exist and, while there is at most one,
61 +// that thread; a shell update of any thread in a populated workspace must not
62 +// re-render the app root.
63 +const threadCountAtom = Atom.make(
64 + (get) => get(environmentThreadShells.threadShellsAtom).length,
65 +).pipe(Atom.withLabel("web-onboarding-thread-count"));
66 +
67 +const soleThreadAtom = Atom.make((get) => {
68 + const threads = get(environmentThreadShells.threadShellsAtom);
69 + return threads.length === 1 ? threads[0]! : null;
70 +}).pipe(Atom.withLabel("web-onboarding-sole-thread"));
71 +
72 export function FirstRunGate({
73 enabled,
74 hostedStatic,
@@ -79,7 +87,8 @@ export function FirstRunGate({
87 const bootstrapped = useAllEnvironmentShellsBootstrapped();
88 const { environments, isReady: environmentCatalogReady } = useEnvironments();
89 const projects = useProjects();
82 - const threads = useThreadShells();
90 + const threadCount = useAtomValue(threadCountAtom);
91 + const soleThread = useAtomValue(soleThreadAtom);
92 const serverConfig = useAtomValue(primaryServerConfigAtom);
93 const serverWelcome = useAtomValue(primaryServerWelcomeAtom);
94 const primaryShellLive = useAtomValue(primaryShellLiveAtom);
@@ -99,16 +108,18 @@ export function FirstRunGate({
108 // would mean `npx t3` users never see the wizard.
109 const serverCwd = serverConfig?.cwd ?? null;
110 const primaryEnvironmentId = serverConfig?.environment.environmentId ?? null;
102 - const workspaceFresh = isFreshFirstRunWorkspace({
103 - primaryEnvironmentId,
104 - serverCwd,
105 - bootstrapProjectId: serverWelcome?.bootstrapProjectId,
106 - bootstrapThreadId: serverWelcome?.bootstrapThreadId,
107 - bootstrapProjectCreated: serverWelcome?.bootstrapProjectCreated,
108 - bootstrapThreadCreated: serverWelcome?.bootstrapThreadCreated,
109 - projects,
110 - threads,
111 - });
111 + const workspaceFresh =
112 + threadCount <= 1 &&
113 + isFreshFirstRunWorkspace({
114 + primaryEnvironmentId,
115 + serverCwd,
116 + bootstrapProjectId: serverWelcome?.bootstrapProjectId,
117 + bootstrapThreadId: serverWelcome?.bootstrapThreadId,
118 + bootstrapProjectCreated: serverWelcome?.bootstrapProjectCreated,
119 + bootstrapThreadCreated: serverWelcome?.bootstrapThreadCreated,
120 + projects,
121 + threads: soleThread === null ? [] : [soleThread],
122 + });
123
124 const { decision: nextDecision, persistCompletion } = hostedStatic
125 ? resolveHostedFirstRunDecision({
@@ -133,7 +144,7 @@ export function FirstRunGate({
144 serverConfigAvailable: serverConfig !== null,
145 workspaceFresh,
146 projectCount: projects.length,
136 - threadCount: threads.length,
147 + threadCount,
148 });
149
150 if (decision !== "wizard" && hydrated) {
apps/web/src/hooks/useThreadVisitedMigration.ts
+14 -6
@@ -1,17 +1,20 @@
1 +import { RegistryContext } from "@effect/atom-react";
2 import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
2 -import { useEffect } from "react";
3 +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4 +import { useContext, useEffect, useEffectEvent } from "react";
5
4 -import { useThreadShells } from "../state/entities";
5 -import { threadEnvironment } from "../state/threads";
6 +import { environmentThreadShells, threadEnvironment } from "../state/threads";
7 import { useAtomCommand } from "../state/use-atom-command";
8 import { useUiStateStore } from "../uiStateStore";
9
10 const migratedThreadKeys = new Set<string>();
11
12 +// Subscribes outside render: the migration only acts on threads it has not
13 +// seen, so a shell update must not re-render the layout that mounts it.
14 export function useThreadVisitedMigration(): void {
12 - const threads = useThreadShells();
15 + const registry = useContext(RegistryContext);
16 const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
14 - useEffect(() => {
17 + const migrate = useEffectEvent((threads: ReadonlyArray<EnvironmentThreadShell>) => {
18 for (const thread of threads) {
19 // Field absent → the environment's server has no visited tracking; keep
20 // the local value in play and reconsider if the server upgrades.
@@ -30,5 +33,10 @@ export function useThreadVisitedMigration(): void {
33 input: { threadId: thread.id, visitedAt: local },
34 });
35 }
33 - }, [threads, visitThreadMutation]);
36 + });
37 + useEffect(
38 + () =>
39 + registry.subscribe(environmentThreadShells.threadShellsAtom, migrate, { immediate: true }),
40 + [registry],
41 + );
42 }
apps/web/src/state/entities.relationshipShells.test.ts new
+67
@@ -0,0 +1,67 @@
1 +import type {
2 + OrchestrationV2ThreadProjection,
3 + OrchestrationV2ThreadShell,
4 +} from "@t3tools/contracts";
5 +import { describe, expect, it } from "vite-plus/test";
6 +
7 +import { selectThreadRelationshipShells } from "./entities";
8 +
9 +type Projection = Parameters<typeof selectThreadRelationshipShells>[1];
10 +
11 +const shell = (id: string, parentThreadId: string | null = null) =>
12 + ({
13 + id,
14 + forkedFrom: null,
15 + lineage: { parentThreadId, relationshipToParent: parentThreadId === null ? null : "fork" },
16 + }) as unknown as OrchestrationV2ThreadShell;
17 +
18 +const threadId = "current" as OrchestrationV2ThreadShell["id"];
19 +
20 +describe("selectThreadRelationshipShells", () => {
21 + it("keeps the thread, its parent, its children and the threads its projection names", () => {
22 + const threads = [
23 + shell("current", "parent"),
24 + shell("parent"),
25 + shell("child", "current"),
26 + shell("subagent-child"),
27 + shell("workflow-member"),
28 + shell("transfer-source"),
29 + shell("unrelated"),
30 + shell("grandchild", "child"),
31 + ];
32 + const projection = {
33 + thread: threads[0],
34 + subagents: [
35 + {
36 + childThreadId: "subagent-child",
37 + workflow: { agents: [{ childThreadId: "workflow-member" }, {}] },
38 + },
39 + { childThreadId: null },
40 + ],
41 + contextTransfers: [{ sourceThreadId: "transfer-source", targetThreadId: "current" }],
42 + } as unknown as Pick<
43 + OrchestrationV2ThreadProjection,
44 + "thread" | "subagents" | "contextTransfers"
45 + >;
46 +
47 + expect(
48 + selectThreadRelationshipShells(threadId, projection, threads).map((thread) => thread.id),
49 + ).toEqual([
50 + "current",
51 + "parent",
52 + "child",
53 + "subagent-child",
54 + "workflow-member",
55 + "transfer-source",
56 + ]);
57 + });
58 +
59 + it("finds the parent from the shell before the projection loads", () => {
60 + const threads = [shell("current", "parent"), shell("parent"), shell("unrelated")];
61 + expect(
62 + selectThreadRelationshipShells(threadId, null as Projection, threads).map(
63 + (thread) => thread.id,
64 + ),
65 + ).toEqual(["current", "parent"]);
66 + });
67 +});
apps/web/src/state/entities.ts
+79 -1
@@ -9,8 +9,19 @@ import {
9 type EnvironmentThreadStatus,
10 type ThreadHistoryMeta,
11 } from "@t3tools/client-runtime/state/threads";
12 +import {
13 + arrayElementsEqual,
14 + parseThreadKey,
15 + threadKey,
16 +} from "@t3tools/client-runtime/state/entities";
17 import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
13 -import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
18 +import type {
19 + EnvironmentId,
20 + OrchestrationV2ProjectedTurnItem,
21 + OrchestrationV2ThreadProjection,
22 + OrchestrationV2ThreadShell,
23 + ThreadId,
24 +} from "@t3tools/contracts";
25 import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints";
26 import { Atom } from "effect/reactivity";
27 import { appAtomRegistry } from "../rpc/atomRegistry";
@@ -312,3 +323,70 @@ export function readEnvironmentThreadRefs(
323 export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
324 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
325 }
326 +
327 +function shellParentThreadId(
328 + thread: Pick<OrchestrationV2ThreadShell, "forkedFrom" | "lineage">,
329 +): ThreadId | null {
330 + return thread.forkedFrom?.type === "run"
331 + ? thread.forkedFrom.threadId
332 + : thread.lineage.parentThreadId;
333 +}
334 +
335 +/**
336 + * The live shells the panel's immediate relationships can reach: the thread,
337 + * its parent, its children, and the threads its subagents, workflow members and
338 + * context transfers name. Keeps its array while those shells are unchanged, so
339 + * another thread streaming in the same environment does not re-render the panel.
340 + */
341 +/** Exported for tests. */
342 +export function selectThreadRelationshipShells(
343 + threadId: ThreadId,
344 + projection: Pick<
345 + OrchestrationV2ThreadProjection,
346 + "thread" | "subagents" | "contextTransfers"
347 + > | null,
348 + threads: ReadonlyArray<OrchestrationV2ThreadShell>,
349 +): ReadonlyArray<OrchestrationV2ThreadShell> {
350 + const named = new Set<string>([threadId]);
351 + if (projection !== null) {
352 + const parentThreadId = shellParentThreadId(projection.thread);
353 + if (parentThreadId !== null) named.add(parentThreadId);
354 + for (const subagent of projection.subagents) {
355 + if (subagent.childThreadId !== null) named.add(subagent.childThreadId);
356 + for (const agent of subagent.workflow?.agents ?? []) {
357 + if (agent.childThreadId !== undefined) named.add(agent.childThreadId);
358 + }
359 + }
360 + for (const transfer of projection.contextTransfers) {
361 + named.add(transfer.sourceThreadId);
362 + named.add(transfer.targetThreadId);
363 + }
364 + }
365 + const current = threads.find((thread) => thread.id === threadId);
366 + const currentParentId = current === undefined ? null : shellParentThreadId(current);
367 + if (currentParentId !== null) named.add(currentParentId);
368 + return threads.filter(
369 + (thread) => named.has(thread.id) || shellParentThreadId(thread) === threadId,
370 + );
371 +}
372 +
373 +const relationshipShellsAtomFamily = Atom.family((key: string) => {
374 + const ref = parseThreadKey(key);
375 + let previous: ReadonlyArray<OrchestrationV2ThreadShell> = [];
376 + return Atom.make((get) => {
377 + const next = selectThreadRelationshipShells(
378 + ref.threadId,
379 + get(environmentThreadDetails.threadAtom(ref))?.projection ?? null,
380 + get(environmentThreadShells.environmentThreadsAtom(ref.environmentId)),
381 + );
382 + if (arrayElementsEqual(previous, next)) return previous;
383 + previous = next;
384 + return next;
385 + }).pipe(Atom.withLabel(`web-thread-relationship-shells:${key}`));
386 +});
387 +
388 +export function useThreadRelationshipShells(
389 + ref: ScopedThreadRef,
390 +): ReadonlyArray<OrchestrationV2ThreadShell> {
391 + return useAtomValue(relationshipShellsAtomFamily(threadKey(ref)));
392 +}
apps/web/src/state/shell.ts
+25
@@ -7,12 +7,14 @@ import {
7 createEnvironmentSnapshotAtom,
8 createShellEnvironmentAtoms,
9 type EnvironmentShellState,
10 + type EnvironmentShellStatus,
11 } from "@t3tools/client-runtime/state/shell";
12 import {
13 type EnvironmentCatalogState,
14 enabledEnvironmentIds,
15 } from "@t3tools/client-runtime/state/connections";
16 import type { EnvironmentId } from "@t3tools/contracts";
17 +import { useAtomValue } from "@effect/atom-react";
18 import * as Option from "effect/Option";
19 import { AsyncResult, Atom } from "effect/reactivity";
20
@@ -24,6 +26,29 @@ export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntim
26 export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
27 export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
28
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 +
52 export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
53 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
54 if (Option.isNone(catalog)) {
apps/web/src/state/usePullRequestStack.ts
+23 -12
@@ -1,28 +1,39 @@
1 -import type { EnvironmentId, PullRequestRef } from "@t3tools/contracts";
1 +import { useAtomValue } from "@effect/atom-react";
2 +import { ThreadPullRequestLink, type EnvironmentId, type PullRequestRef } from "@t3tools/contracts";
3 +import { Atom } from "effect/reactivity";
4 +import * as Schema from "effect/Schema";
5 import {
6 savedPullRequestStack,
7 pullRequestStackView,
8 } from "../components/pullRequest/pullRequestStackSnapshot";
6 -import { useThreadShells } from "./entities";
9 import { pullRequestStackAtom } from "./pullRequests";
10 import { useEnvironmentQuery } from "./query";
11 +import { environmentThreadShells } from "./threads";
12 +
13 +const sameLinks = Schema.toEquivalence(Schema.Array(ThreadPullRequestLink));
14 +
15 +// A shell update re-sends a thread's links as a new array, so equal links keep
16 +// the previous list and a streaming thread does not re-render the stack.
17 +const environmentPullRequestLinksAtom = Atom.family((environmentId: EnvironmentId) => {
18 + let previous: ReadonlyArray<ThreadPullRequestLink> = [];
19 + return Atom.make((get) => {
20 + const next = get(environmentThreadShells.environmentThreadsAtom(environmentId)).flatMap(
21 + (thread) => thread.pullRequests ?? [],
22 + );
23 + if (sameLinks(previous, next)) return previous;
24 + previous = next;
25 + return next;
26 + }).pipe(Atom.withLabel(`web-environment-pull-request-links:${environmentId}`));
27 +});
28
29 export function usePullRequestStack(
30 environmentId: EnvironmentId,
31 reference: PullRequestRef | null,
32 ) {
14 - const threads = useThreadShells();
33 + const links = useAtomValue(environmentPullRequestLinksAtom(environmentId));
34 + const saved = reference === null ? null : savedPullRequestStack(links, reference);
35 const query = useEnvironmentQuery(
36 reference === null ? null : pullRequestStackAtom({ environmentId, input: reference }),
37 );
18 - const saved =
19 - reference === null
20 - ? null
21 - : savedPullRequestStack(
22 - threads
23 - .filter((thread) => thread.environmentId === environmentId)
24 - .flatMap((thread) => thread.pullRequests ?? []),
25 - reference,
26 - );
38 return { ...query, ...pullRequestStackView(query, saved) };
39 }
apps/web/src/threadRoutes.ts
+1 -1
@@ -22,7 +22,7 @@ type DraftThreadRouteState = {
22 export type ThreadRouteRenderState = "loading" | "ready" | "missing";
23
24 export function isThreadRouteSnapshotAuthoritative(
25 - status: EnvironmentShellStatus | undefined,
25 + status: EnvironmentShellStatus | null | undefined,
26 ): boolean {
27 return status === "live";
28 }
packages/client-runtime/src/state/shell.test.ts
+36 -1
@@ -7,7 +7,11 @@ import { Atom, AtomRegistry } from "effect/reactivity";
7
8 import { PrimaryConnectionTarget } from "../connection/model.ts";
9 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";
15 import { v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
16
17 const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
@@ -231,4 +235,35 @@ describe("environment shell projections", () => {
235 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
236 expect(harness.registry.get(harness.serverConfigsAtom)).toBe(withConfig);
237 });
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 + });
269 });
packages/client-runtime/src/state/shell.ts
+25
@@ -522,6 +522,27 @@ export function createEnvironmentServerConfigsAtom(input: {
522 }).pipe(Atom.withLabel("environment-server-configs"));
523 }
524
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 +
546 export function createEnvironmentShellAtoms<R, E>(
547 runtime: Atom.AtomRuntime<
548 | EnvironmentRegistry.EnvironmentRegistry
@@ -543,9 +564,13 @@ export function createEnvironmentShellAtoms<R, E>(
564 ).pipe(Atom.withLabel(`environment-shell-state-value:${environmentId}`)),
565 );
566
567 + const { statusAtom, hasSnapshotAtom } = createEnvironmentShellProgressAtoms(stateValueAtom);
568 +
569 return {
570 stateAtom,
571 stateValueAtom,
572 + statusAtom,
573 + hasSnapshotAtom,
574 };
575 }
576