TypeScript
fix(web): a new thread's first reply shows without reloading (#27522)
Julius Marminge8d012a688e7c
diff --git a/apps/web/src/components/ChatView.browser.test.tsx b/apps/web/src/components/ChatView.browser.test.tsxnew file mode 100644index 00000000000..8fe98b78da1--- /dev/null+++ b/apps/web/src/components/ChatView.browser.test.tsx@@ -0,0 +1,64 @@+import { createMemoryHistory } from "@tanstack/react-router";+import { scopeProjectRef } from "@t3tools/client-runtime/environment";+import { ThreadId } from "@t3tools/contracts";+import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";+import type * as Context from "effect/Context";+import { expect, it, vi } from "vite-plus/test";+import { page } from "vite-plus/test/browser";++import { AppRoot } from "~/AppRoot";+import { DraftId, useComposerDraftStore } from "~/composerDraftStore";+import { __setClientSettingsForTests } from "~/hooks/useSettings";+import { getRouter } from "~/router";+import {+ ENVIRONMENT_ID,+ PROJECT_ID,+ installFakeEnvironment,+ type FakeDriver,+} from "../../test/fakeEnvironment";+import { render } from "../../test/render";++// The socket is the only fake layer; see test/appScenarios.renders.tsx.+vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {+ const [actual, { fakeDriver }, Layer] = await Promise.all([+ importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),+ import("../../test/fakeEnvironment"),+ import("effect/Layer"),+ ]);+ return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };+});++vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({+ ...(await original()),+ trackRpcRequestSent: () => {},+}));++it("a new draft does not load its reserved thread before the server lists it", async () => {+ using _environment = installFakeEnvironment([]);+ using fetchSpy = vi.spyOn(globalThis, "fetch");+ __setClientSettingsForTests({+ ...DEFAULT_CLIENT_SETTINGS,+ onboardingCompletedAt: "2026-01-01T00:00:00.000Z",+ });+ const draftId = DraftId.make("draft-27443");+ const threadId = ThreadId.make("thread-27443");+ useComposerDraftStore+ .getState()+ .setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { threadId });++ const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));+ await router.load();+ await using _app = await render(<AppRoot router={router} />, {+ // AppRoot brings its own router.+ router: false,+ });+ await expect.element(page.getByTestId("composer-editor")).toBeVisible();+ // The shell snapshot has arrived once the sidebar lists the project, so the connection is up.+ await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();++ const threadRequests = fetchSpy.mock.calls+ .map(([input]) => String(input instanceof Request ? input.url : input))+ .filter((url) => url.includes(`/api/orchestration/threads/${threadId}/`));+ // A 404 here marks the thread deleted, and the view never loads it once the send creates it.+ expect(threadRequests).toEqual([]);+});diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex cf48ddcb842..41714993675 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -2376,6 +2376,7 @@ export default function ChatView(props: ChatViewProps) {activeThread,activeThreadId,activeThreadRef,+ threadDetailRef: routeThreadDetailRef,activeThreadKey,activeThreadShell,activeLatestRun,diff --git a/apps/web/src/components/chat/useThreadLifecycle.tsx b/apps/web/src/components/chat/useThreadLifecycle.tsxindex 0ad8acacf71..53c33b5b1ad 100644--- a/apps/web/src/components/chat/useThreadLifecycle.tsx+++ b/apps/web/src/components/chat/useThreadLifecycle.tsx@@ -87,6 +87,8 @@ interface ThreadLifecycleInput {readonly activeThread: EnvironmentThreadShell | undefined;readonly activeThreadId: ThreadId | null;readonly activeThreadRef: ScopedThreadRef | null;+ /** Null until the thread's detail may load; see resolveThreadDetailRef. */+ readonly threadDetailRef: ScopedThreadRef | null;readonly activeThreadKey: string | null;readonly activeThreadShell: EnvironmentThreadShell | null;readonly activeLatestRun: EnvironmentThreadShell["latestRun"];@@ -138,6 +140,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {activeThread,activeThreadId,activeThreadRef,+ threadDetailRef,activeThreadKey,activeThreadShell,activeLatestRun,@@ -524,7 +527,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {const forceStop = useForceStopThread(environmentId);const anyRunStop = useAtomValue(- activeThreadRef ? environmentThreadDetails.runStopAtom(activeThreadRef) : NO_RUN_STOP_ATOM,+ threadDetailRef ? environmentThreadDetails.runStopAtom(threadDetailRef) : NO_RUN_STOP_ATOM,);// A background Stop matters only while that work is still pending.const runStop = anyRunStop?.background && activeBackgroundTasks.length === 0 ? null : anyRunStop;diff --git a/apps/web/test/fakeEnvironment.ts b/apps/web/test/fakeEnvironment.tsindex 26e2ee9fa33..c586ad51c2c 100644--- a/apps/web/test/fakeEnvironment.ts+++ b/apps/web/test/fakeEnvironment.ts@@ -44,7 +44,7 @@ import * as Stream from "effect/Stream";import { RpcSchema } from "effect/rpc";export const ENVIRONMENT_ID = EnvironmentId.make("environment-render");-const PROJECT_ID = ProjectId.make("project-render");+export const PROJECT_ID = ProjectId.make("project-render");const CODEX = ProviderInstanceId.make("codex");const MODEL = { instanceId: CODEX, model: "gpt-5.4" };const AT = DateTime.makeUnsafe("2026-03-17T19:12:28.000Z");