Skip to content

Commit 8d012a6

Browse files
fix(web): a new thread's first reply shows without reloading (#27522)
1 parent 028ed85 commit 8d012a6

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { createMemoryHistory } from "@tanstack/react-router";
2+
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3+
import { ThreadId } from "@t3tools/contracts";
4+
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
5+
import type * as Context from "effect/Context";
6+
import { expect, it, vi } from "vite-plus/test";
7+
import { page } from "vite-plus/test/browser";
8+
9+
import { AppRoot } from "~/AppRoot";
10+
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
11+
import { __setClientSettingsForTests } from "~/hooks/useSettings";
12+
import { getRouter } from "~/router";
13+
import {
14+
ENVIRONMENT_ID,
15+
PROJECT_ID,
16+
installFakeEnvironment,
17+
type FakeDriver,
18+
} from "../../test/fakeEnvironment";
19+
import { render } from "../../test/render";
20+
21+
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
22+
vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
23+
const [actual, { fakeDriver }, Layer] = await Promise.all([
24+
importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
25+
import("../../test/fakeEnvironment"),
26+
import("effect/Layer"),
27+
]);
28+
return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
29+
});
30+
31+
vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
32+
...(await original()),
33+
trackRpcRequestSent: () => {},
34+
}));
35+
36+
it("a new draft does not load its reserved thread before the server lists it", async () => {
37+
using _environment = installFakeEnvironment([]);
38+
using fetchSpy = vi.spyOn(globalThis, "fetch");
39+
__setClientSettingsForTests({
40+
...DEFAULT_CLIENT_SETTINGS,
41+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
42+
});
43+
const draftId = DraftId.make("draft-27443");
44+
const threadId = ThreadId.make("thread-27443");
45+
useComposerDraftStore
46+
.getState()
47+
.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { threadId });
48+
49+
const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
50+
await router.load();
51+
await using _app = await render(<AppRoot router={router} />, {
52+
// AppRoot brings its own router.
53+
router: false,
54+
});
55+
await expect.element(page.getByTestId("composer-editor")).toBeVisible();
56+
// The shell snapshot has arrived once the sidebar lists the project, so the connection is up.
57+
await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();
58+
59+
const threadRequests = fetchSpy.mock.calls
60+
.map(([input]) => String(input instanceof Request ? input.url : input))
61+
.filter((url) => url.includes(`/api/orchestration/threads/${threadId}/`));
62+
// A 404 here marks the thread deleted, and the view never loads it once the send creates it.
63+
expect(threadRequests).toEqual([]);
64+
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,7 @@ export default function ChatView(props: ChatViewProps) {
23762376
activeThread,
23772377
activeThreadId,
23782378
activeThreadRef,
2379+
threadDetailRef: routeThreadDetailRef,
23792380
activeThreadKey,
23802381
activeThreadShell,
23812382
activeLatestRun,

‎apps/web/src/components/chat/useThreadLifecycle.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ interface ThreadLifecycleInput {
8787
readonly activeThread: EnvironmentThreadShell | undefined;
8888
readonly activeThreadId: ThreadId | null;
8989
readonly activeThreadRef: ScopedThreadRef | null;
90+
/** Null until the thread's detail may load; see resolveThreadDetailRef. */
91+
readonly threadDetailRef: ScopedThreadRef | null;
9092
readonly activeThreadKey: string | null;
9193
readonly activeThreadShell: EnvironmentThreadShell | null;
9294
readonly activeLatestRun: EnvironmentThreadShell["latestRun"];
@@ -138,6 +140,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
138140
activeThread,
139141
activeThreadId,
140142
activeThreadRef,
143+
threadDetailRef,
141144
activeThreadKey,
142145
activeThreadShell,
143146
activeLatestRun,
@@ -524,7 +527,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
524527

525528
const forceStop = useForceStopThread(environmentId);
526529
const anyRunStop = useAtomValue(
527-
activeThreadRef ? environmentThreadDetails.runStopAtom(activeThreadRef) : NO_RUN_STOP_ATOM,
530+
threadDetailRef ? environmentThreadDetails.runStopAtom(threadDetailRef) : NO_RUN_STOP_ATOM,
528531
);
529532
// A background Stop matters only while that work is still pending.
530533
const runStop = anyRunStop?.background && activeBackgroundTasks.length === 0 ? null : anyRunStop;

‎apps/web/test/fakeEnvironment.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import * as Stream from "effect/Stream";
4444
import { RpcSchema } from "effect/rpc";
4545

4646
export const ENVIRONMENT_ID = EnvironmentId.make("environment-render");
47-
const PROJECT_ID = ProjectId.make("project-render");
47+
export const PROJECT_ID = ProjectId.make("project-render");
4848
const CODEX = ProviderInstanceId.make("codex");
4949
const MODEL = { instanceId: CODEX, model: "gpt-5.4" };
5050
const AT = DateTime.makeUnsafe("2026-03-17T19:12:28.000Z");

0 commit comments

Comments
 (0)