Skip to content
TypeScript

fix(web): opening a just-created thread no longer bounces to the start page (#19724)

FranzChab638a6e0d6a
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index d1baa2cafb9..c56aa62f607 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -88,6 +88,7 @@ import {
import { readPastedComposerContext } from "./composerInlineTokenPaste";
import { isPasteAsTextShortcut } from "@t3tools/client-runtime/text-paste";
import { effectiveSnoozed, threadWokeAt } from "@t3tools/client-runtime/state/thread-settled";
+import { presentThreadShell } from "@t3tools/client-runtime/state/shell";
import { childThreadInputsTitle } from "@t3tools/client-runtime/state/threads";
import { useAcknowledgeThreadWoke, useThreadActions } from "../hooks/useThreadActions";
import {
@@ -1120,13 +1121,55 @@ export default function ChatView(props: ChatViewProps) {
? store.getDraftSession(draftId)
: null,
);
- const serverThread = useQuietThreadShell(routeThreadRef);
+ const serverThreadShell = useQuietThreadShell(routeThreadRef);
const routeThreadDetailRef = resolveThreadDetailRef(routeThreadRef, {
- shellExists: serverThread !== null,
+ shellExists: serverThreadShell !== null,
waitForShell: draftThread !== null,
});
const serverThreadProjection = useThreadProjection(routeThreadDetailRef);
const serverProjection = serverThreadProjection?.projection ?? null;
+ const serverThread = (() => {
+ if (serverThreadShell !== null || serverProjection === null) {
+ return serverThreadShell;
+ }
+ const latestRun = deriveLatestThreadRun(serverProjection);
+ const runtime = deriveThreadRuntime(serverProjection);
+ const pendingRuntimeRequest =
+ serverProjection.runtimeRequests.findLast((request) => request.status === "pending") ?? null;
+ // Direct links can hydrate detail before the thread enters the shell index.
+ return {
+ ...presentThreadShell(environmentId, {
+ ...serverProjection.thread,
+ latestRunId: latestRun?.runId ?? null,
+ activeRunId: runtime?.activeRunId ?? null,
+ status: runtime?.status ?? "idle",
+ pendingRuntimeRequest,
+ latestVisibleMessage: null,
+ latestUserMessageAt:
+ serverProjection.messages.findLast((message) => message.role === "user")?.updatedAt ??
+ null,
+ hasActionableProposedPlan: serverProjection.plans.some(
+ (plan) => plan.kind === "proposed_plan" && plan.status === "active",
+ ),
+ providerInstanceHistory: [
+ ...new Set(
+ serverProjection.providerThreads
+ .filter((thread) => thread.appThreadId === threadId && thread.ownerNodeId === null)
+ .map((thread) => thread.providerInstanceId),
+ ),
+ ],
+ goal:
+ serverProjection.providerThreads.find(
+ (thread) => thread.id === serverProjection.thread.activeProviderThreadId,
+ )?.goal ?? null,
+ itemCount: serverProjection.turnItems.length,
+ visibleItemCount: serverProjection.visibleTurnItems.length,
+ updatedAt: serverProjection.updatedAt,
+ }),
+ latestRun,
+ runtime,
+ };
+ })();
const promptRef = useRef("");
const localComposerRef = useRef<ChatComposerHandle | null>(null);
const composerRef = useComposerHandleContext() ?? localComposerRef;
@@ -1608,7 +1651,7 @@ export default function ChatView(props: ChatViewProps) {
container: workspaceLayoutElement,
widthStorageKey: `t3code:preview-panel-width:${activeThreadKey}`,
});
- const activeThreadShell = useQuietThreadShell(isServerThread ? activeThreadRef : null);
+ const activeThreadShell = serverThread;
const timelineThreadError =
serverRuntime?.status === "failed" &&
serverRuntime.lastErrorClass === "usage_limit" &&
diff --git a/apps/web/src/components/ThreadRouteView.tsx b/apps/web/src/components/ThreadRouteView.tsx
index 6bbe65761fb..df6e5b476b7 100644
--- a/apps/web/src/components/ThreadRouteView.tsx
+++ b/apps/web/src/components/ThreadRouteView.tsx
@@ -16,6 +16,7 @@ import {
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
import { useEnvironmentThreadRefs, useThreadRefs, useQuietThreadShell } from "../state/entities";
import { useEnvironmentShellStatus } from "../state/shell";
+import { useEnvironmentThread } from "../state/threads";
import {
buildThreadRouteParams,
isThreadRouteSnapshotAuthoritative,
@@ -87,10 +88,22 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
const environmentHasDraftThreads = useComposerDraftStore((store) =>
serverThreadRef ? store.hasDraftThreadsInEnvironment(serverThreadRef.environmentId) : false,
);
+ // Until the shell lists the thread, its own load (HTTP, then the socket)
+ // decides whether it exists. Read only then: once listed, ChatView keeps
+ // this stream open and its updates need not re-render the route.
+ const unlistedThreadRef =
+ target.kind === "server" && serverThreadShell === null && draftThread === null
+ ? target.threadRef
+ : null;
+ const unlistedThreadDetail = useEnvironmentThread(
+ unlistedThreadRef?.environmentId ?? null,
+ unlistedThreadRef?.threadId ?? null,
+ );
const renderState = resolveThreadRouteRenderState({
bootstrapComplete,
serverThreadExists: serverThreadShell !== null,
serverThreadDeleted: serverThreadShell?.deletedAt != null,
+ unlistedThreadDetail,
draftThreadExists: draftThread !== null,
});
const serverThreadStarted = threadHasStarted(serverThreadShell);
diff --git a/apps/web/src/threadRoutes.test.ts b/apps/web/src/threadRoutes.test.ts
index b89e8dd942e..6086ad2b88e 100644
--- a/apps/web/src/threadRoutes.test.ts
+++ b/apps/web/src/threadRoutes.test.ts
@@ -1,7 +1,11 @@
import { describe, expect, it } from "vite-plus/test";
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
+import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
import { ThreadId } from "@t3tools/contracts";
+import * as DateTime from "effect/DateTime";
+import * as Option from "effect/Option";
import { DraftId } from "./composerDraftStore";
+import { makeThreadProjectionFixture } from "./test-fixtures";
import {
buildDraftThreadRouteParams,
@@ -122,6 +126,7 @@ describe("threadRoutes", () => {
bootstrapComplete: true,
serverThreadExists: true,
serverThreadDeleted: false,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
draftThreadExists: false,
}),
).toBe("ready");
@@ -133,6 +138,7 @@ describe("threadRoutes", () => {
bootstrapComplete: true,
serverThreadExists: true,
serverThreadDeleted: false,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
draftThreadExists: false,
}),
).toBe("ready");
@@ -141,6 +147,7 @@ describe("threadRoutes", () => {
bootstrapComplete: true,
serverThreadExists: false,
serverThreadDeleted: false,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
draftThreadExists: true,
}),
).toBe("ready");
@@ -159,6 +166,7 @@ describe("threadRoutes", () => {
bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
serverThreadExists: source === "server thread",
serverThreadDeleted: false,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
draftThreadExists: source === "draft",
}),
).toBe("ready");
@@ -171,6 +179,7 @@ describe("threadRoutes", () => {
bootstrapComplete: false,
serverThreadExists: false,
serverThreadDeleted: false,
+ unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
draftThreadExists: false,
}),
).toBe("loading");
@@ -179,17 +188,108 @@ describe("threadRoutes", () => {
bootstrapComplete: true,
serverThreadExists: false,
serverThreadDeleted: false,
+ unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
draftThreadExists: false,
}),
).toBe("missing");
});
+ it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => {
+ const route = {
+ bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
+ serverThreadExists: false,
+ serverThreadDeleted: false,
+ draftThreadExists: false,
+ };
+
+ expect(
+ resolveThreadRouteRenderState({
+ ...route,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
+ }),
+ ).toBe("loading");
+ expect(
+ resolveThreadRouteRenderState({
+ ...route,
+ unlistedThreadDetail: {
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
+ status: "synchronizing",
+ },
+ }),
+ ).toBe("loading");
+ expect(
+ resolveThreadRouteRenderState({
+ ...route,
+ unlistedThreadDetail: {
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
+ status: "synchronizing",
+ error: Option.some("Connection interrupted"),
+ },
+ }),
+ ).toBe("loading");
+ expect(
+ resolveThreadRouteRenderState({
+ ...route,
+ unlistedThreadDetail: {
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
+ status: "live",
+ data: Option.some(makeThreadProjectionFixture()),
+ },
+ }),
+ ).toBe("ready");
+ });
+
+ it.each(["missing", "deleted", "archived"] as const)(
+ "redirects an unlisted thread only after detail confirms it is %s",
+ (outcome) => {
+ const route = {
+ bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
+ serverThreadExists: false,
+ serverThreadDeleted: false,
+ draftThreadExists: false,
+ };
+ expect(
+ resolveThreadRouteRenderState({
+ ...route,
+ unlistedThreadDetail: {
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
+ status: "synchronizing",
+ },
+ }),
+ ).toBe("loading");
+
+ const projection = makeThreadProjectionFixture();
+ const removedAt = DateTime.makeUnsafe("2026-01-02T00:00:00Z");
+ expect(
+ resolveThreadRouteRenderState({
+ ...route,
+ unlistedThreadDetail:
+ outcome === "missing"
+ ? { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" }
+ : {
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
+ status: "live",
+ data: Option.some({
+ ...projection,
+ thread: {
+ ...projection.thread,
+ deletedAt: outcome === "deleted" ? removedAt : null,
+ archivedAt: outcome === "archived" ? removedAt : null,
+ },
+ }),
+ },
+ }),
+ ).toBe("missing");
+ },
+ );
+
it("redirects deleted server threads", () => {
expect(
resolveThreadRouteRenderState({
bootstrapComplete: true,
serverThreadExists: true,
serverThreadDeleted: true,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
draftThreadExists: false,
}),
).toBe("missing");
@@ -201,6 +301,7 @@ describe("threadRoutes", () => {
bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
serverThreadExists: true,
serverThreadDeleted: true,
+ unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
draftThreadExists: false,
}),
).toBe("loading");
diff --git a/apps/web/src/threadRoutes.ts b/apps/web/src/threadRoutes.ts
index 20b3f7396db..9a1cbef5575 100644
--- a/apps/web/src/threadRoutes.ts
+++ b/apps/web/src/threadRoutes.ts
@@ -1,6 +1,8 @@
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
+import type { EnvironmentThreadState } from "@t3tools/client-runtime/state/threads";
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
+import * as Option from "effect/Option";
import type { DraftId } from "./composerDraftStore";
export type ThreadRouteTarget =
@@ -31,6 +33,7 @@ export function resolveThreadRouteRenderState(input: {
bootstrapComplete: boolean;
serverThreadExists: boolean;
serverThreadDeleted: boolean;
+ unlistedThreadDetail: EnvironmentThreadState;
draftThreadExists: boolean;
}): ThreadRouteRenderState {
if (input.draftThreadExists || (input.serverThreadExists && !input.serverThreadDeleted)) {
@@ -39,7 +42,17 @@ export function resolveThreadRouteRenderState(input: {
if (!input.bootstrapComplete) {
return "loading";
}
- return "missing";
+ if (input.serverThreadExists || input.unlistedThreadDetail.status === "deleted") {
+ return "missing";
+ }
+ // Shell membership can lag a new thread; its detail decides.
+ const projection = Option.getOrNull(input.unlistedThreadDetail.data);
+ if (projection === null) {
+ return "loading";
+ }
+ return projection.thread.deletedAt !== null || projection.thread.archivedAt !== null
+ ? "missing"
+ : "ready";
}
export function buildThreadRouteParams(ref: ScopedThreadRef): {