Skip to content

Commit ead5049

Browse files
fix(web): a draft finalized elsewhere still leads to its thread
Route changes pruned promotion redirects, so a draft the store finalized while the user was on another route (a restored failed send, a background send) lost its redirect, and "Open draft" or Back to /draft/<id> landed on home. Redirects now stay for the page session; the ChatView key latch that needed the pruning moves into ThreadRouteView and lasts only for the handoff. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 840c10c commit ead5049

4 files changed

Lines changed: 50 additions & 96 deletions

File tree

‎apps/web/src/appInit.tsx‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { startKeybindingsUpdateToasts } from "./components/KeybindingsUpdateToas
1515
import { startLegacyThreadMigrationToast } from "./components/LegacyThreadMigrationToast";
1616
import { startPermissionUpdateNotices } from "./components/PermissionUpdateNotice";
1717
import { startAgentOpenedPreviews } from "./components/preview/agentOpenedPreviews";
18-
import { isAppModalPage, startMainAppLocationTracking } from "./components/sidebar/mainAppLocation";
18+
import { startMainAppLocationTracking } from "./components/sidebar/mainAppLocation";
1919
import { startDesktopUpdateLaunchNotice } from "./components/sidebar/updateEverythingRun";
2020
import { startSlowRpcRequestToasts } from "./components/SlowRpcRequestToastCoordinator";
2121
import { startThreadNotifications } from "./components/ThreadNotificationCoordinator";
@@ -142,25 +142,6 @@ function startThreadOverlayRetention(router: AppRouter): void {
142142
});
143143
}
144144

145-
/**
146-
* Drafts promote as their server threads arrive, wherever the user is. The redirect a finalized
147-
* draft leaves only serves the route that shows that draft or its thread.
148-
*/
149-
function startDraftThreadPromotion(router: AppRouter): void {
150-
subscribeDraftThreadPromotion({
151-
registry: appAtomRegistry,
152-
threadShellAtom: environmentThreadShells.quietThreadShellAtom,
153-
threadRefsAtom: environmentThreadShells.threadRefsAtom,
154-
});
155-
router.subscribe("onResolved", () => {
156-
const target = resolveThreadRouteTarget(router.state.matches.at(-1)?.params ?? {});
157-
useComposerDraftStore.getState().retainPromotedDraftRedirectsForRoute({
158-
modalPage: isAppModalPage(router.state.location.pathname),
159-
target,
160-
});
161-
});
162-
}
163-
164145
/**
165146
* Starts every app-lifetime service once, before the first render, and returns the router.
166147
* They run for the life of the page, so their disposers are dropped here.
@@ -191,7 +172,12 @@ export function startAppServices(): AppRouter {
191172
startBrowserProfileReporting();
192173
installChatRouteCommands(router);
193174
startThreadOverlayRetention(router);
194-
startDraftThreadPromotion(router);
175+
// Drafts promote as their server threads arrive, wherever the user is.
176+
subscribeDraftThreadPromotion({
177+
registry: appAtomRegistry,
178+
threadShellAtom: environmentThreadShells.quietThreadShellAtom,
179+
threadRefsAtom: environmentThreadShells.threadRefsAtom,
180+
});
195181
startMainAppLocationTracking(router);
196182
startBackgroundThreadAnnouncements(appAtomRegistry, router);
197183
startThreadVisitedMigration(appAtomRegistry);

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
12
import { Navigate, useNavigate } from "@tanstack/react-router";
2-
import { useEffect } from "react";
3+
import { useEffect, useState } from "react";
34

45
import ChatView from "./ChatView";
56
import { BackgroundThreadContext } from "./chat/backgroundThread";
@@ -45,12 +46,20 @@ export function ThreadRouteView({
4546
const draftThread = useComposerDraftStore((store) =>
4647
serverThreadRef ? store.getDraftThreadByRef(serverThreadRef) : null,
4748
);
48-
// The ChatView that carried a draft keeps its identity once the thread route takes over.
49-
const promotedDraftId = useComposerDraftStore((store) =>
50-
serverThreadRef
51-
? (store.getDraftIdByRef(serverThreadRef) ?? store.getPromotedDraftIdByRef(serverThreadRef))
52-
: null,
53-
);
49+
// The ChatView that carried a draft keeps its key through the redirect to its thread, and only
50+
// until the route leaves that thread, so later visits reuse the shared unkeyed ChatView.
51+
const [handoffKey, setHandoffKey] = useState<{ threadKey: string; key: string } | null>(null);
52+
const nextHandoffKey =
53+
draftId !== null && draftSession === null && draftRedirect
54+
? handoffKey?.key === draftId
55+
? handoffKey
56+
: { threadKey: scopedThreadKey(draftRedirect), key: draftId }
57+
: serverThreadRef && handoffKey?.threadKey === scopedThreadKey(serverThreadRef)
58+
? handoffKey
59+
: null;
60+
if (nextHandoffKey !== handoffKey) {
61+
setHandoffKey(nextHandoffKey);
62+
}
5463
const environmentHasDraftThreads = useComposerDraftStore((store) =>
5564
serverThreadRef ? store.hasDraftThreadsInEnvironment(serverThreadRef.environmentId) : false,
5665
);
@@ -124,7 +133,7 @@ export function ThreadRouteView({
124133
} else if (renderState === "ready" || (renderState === "loading" && serverThreadShell !== null)) {
125134
view = (
126135
<ChatView
127-
key={promotedDraftId ?? undefined}
136+
key={nextHandoffKey?.key}
128137
environmentId={target.threadRef.environmentId}
129138
threadId={target.threadRef.threadId}
130139
routeKind="server"

‎apps/web/src/composerDraftStore.test.ts‎

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,44 +1428,9 @@ describe("composerDraftStore draft promotion", () => {
14281428
const state = useComposerDraftStore.getState();
14291429
expect(state.getDraftSession(draftId)).toBeNull();
14301430
expect(state.getPromotedDraftRedirect(draftId)).toEqual(threadRef);
1431-
expect(state.getPromotedDraftIdByRef(threadRef)).toBe(draftId);
14321431
expect(draftFor(threadId, TEST_ENVIRONMENT_ID)?.prompt).toBe("typed during setup");
14331432
});
14341433

1435-
it("keeps a promotion redirect only while the route shows the draft or its thread", () => {
1436-
const otherRef = scopeThreadRef(TEST_ENVIRONMENT_ID, ThreadId.make("thread-other"));
1437-
useComposerDraftStore.setState({ promotedDraftRedirects: { [draftId]: threadRef } });
1438-
const store = useComposerDraftStore.getState();
1439-
1440-
store.retainPromotedDraftRedirectsForRoute({
1441-
modalPage: false,
1442-
target: { kind: "draft", draftId },
1443-
});
1444-
store.retainPromotedDraftRedirectsForRoute({
1445-
modalPage: false,
1446-
target: { kind: "server", threadRef },
1447-
});
1448-
expect(useComposerDraftStore.getState().getPromotedDraftIdByRef(threadRef)).toBe(draftId);
1449-
1450-
store.retainPromotedDraftRedirectsForRoute({
1451-
modalPage: false,
1452-
target: { kind: "server", threadRef: otherRef },
1453-
});
1454-
expect(useComposerDraftStore.getState().promotedDraftRedirects).toEqual({});
1455-
// A later visit renders the shared unkeyed ChatView.
1456-
expect(useComposerDraftStore.getState().getPromotedDraftIdByRef(threadRef)).toBeNull();
1457-
});
1458-
1459-
it("keeps a promotion redirect while Settings or Usage is open over its draft", () => {
1460-
useComposerDraftStore.setState({ promotedDraftRedirects: { [draftId]: threadRef } });
1461-
const store = useComposerDraftStore.getState();
1462-
1463-
// Each settings section is its own route with no thread target; closing returns to the draft.
1464-
store.retainPromotedDraftRedirectsForRoute({ modalPage: true, target: null });
1465-
store.retainPromotedDraftRedirectsForRoute({ modalPage: true, target: null });
1466-
expect(useComposerDraftStore.getState().getPromotedDraftRedirect(draftId)).toEqual(threadRef);
1467-
});
1468-
14691434
it("holds a background submission on its draft until the send settles", async () => {
14701435
const h = draftPromotionHarness();
14711436
const store = useComposerDraftStore.getState();
@@ -1486,6 +1451,30 @@ describe("composerDraftStore draft promotion", () => {
14861451
expect(useComposerDraftStore.getState().getDraftSession(draftId)).toBeNull();
14871452
});
14881453

1454+
it("keeps the redirect of a draft finalized while another route was open", async () => {
1455+
// A failed send restored onto its existing thread, or a background send, finalizes while the
1456+
// user is elsewhere; "Open draft" or Back to /draft/<id> later must still reach the thread.
1457+
const h = draftPromotionHarness();
1458+
const store = useComposerDraftStore.getState();
1459+
store.setProjectDraftThreadId(projectRef, draftId, { threadId });
1460+
h.arrive(threadRef, {
1461+
latestRun: preparingRun,
1462+
latestUserMessageAt: "2026-03-29T00:00:00.000Z",
1463+
});
1464+
await waitForDraftHeroTransition();
1465+
expect(useComposerDraftStore.getState().getDraftSession(draftId)).toBeNull();
1466+
1467+
const laterDraftId = DraftId.make("draft-later");
1468+
const laterThreadId = ThreadId.make("thread-later");
1469+
store.setProjectDraftThreadId(projectRef, laterDraftId, { threadId: laterThreadId });
1470+
h.arrive(scopeThreadRef(TEST_ENVIRONMENT_ID, laterThreadId), {
1471+
latestRun: preparingRun,
1472+
latestUserMessageAt: "2026-03-29T00:00:00.000Z",
1473+
});
1474+
await waitForDraftHeroTransition();
1475+
expect(useComposerDraftStore.getState().getPromotedDraftRedirect(draftId)).toEqual(threadRef);
1476+
});
1477+
14891478
it("does not finalize a draft whose send failed and was restored", async () => {
14901479
const h = draftPromotionHarness();
14911480
const store = useComposerDraftStore.getState();

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import type {
5252
import { waitForDraftHeroTransition } from "./components/chat/draftHeroTransition";
5353
import { getLocalStorageItem } from "./hooks/useLocalStorage";
5454
import { readThreadPreviewState } from "./previewStateStore";
55-
import type { ThreadRouteTarget } from "./threadRoutes";
5655
import { resolveAppModelSelection, resolveAppModelSelectionForInstance } from "./modelSelection";
5756
import {
5857
DEFAULT_INTERACTION_MODE,
@@ -419,9 +418,8 @@ interface ComposerDraftStoreState {
419418
logicalProjectDraftThreadKeyByLogicalProjectKey: Record<string, string>;
420419
backgroundSubmissionThreadKeys: Record<string, true>;
421420
/**
422-
* Where a just-finalized draft went, so its route and ChatView follow it. Only the handoff
423-
* needs it: `retainPromotedDraftRedirectsForRoute` drops entries the open route no longer
424-
* shows, so a later visit to the thread renders the shared unkeyed ChatView.
421+
* Where each finalized draft went, so `/draft/<id>` keeps leading to its thread (Back, an
422+
* "Open draft" toast) after the draft is gone. In memory only, one small entry per sent draft.
425423
*/
426424
promotedDraftRedirects: Record<string, ScopedThreadRef>;
427425
rewindingThreadKeys: ReadonlySet<string>;
@@ -439,11 +437,6 @@ interface ComposerDraftStoreState {
439437
getDraftSessionByRef: (threadRef: ScopedThreadRef) => DraftSessionState | null;
440438
getDraftIdByRef: (threadRef: ScopedThreadRef) => DraftId | null;
441439
getPromotedDraftRedirect: (draftId: DraftId) => ScopedThreadRef | null;
442-
getPromotedDraftIdByRef: (threadRef: ScopedThreadRef) => DraftId | null;
443-
retainPromotedDraftRedirectsForRoute: (route: {
444-
modalPage: boolean;
445-
target: ThreadRouteTarget | null;
446-
}) => void;
447440
getDraftThreadByRef: (threadRef: ScopedThreadRef) => DraftThreadState | null;
448441
getDraftThread: (threadRef: ComposerThreadTarget) => DraftThreadState | null;
449442
listDraftThreadKeys: () => string[];
@@ -2488,29 +2481,6 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
24882481
return null;
24892482
},
24902483
getPromotedDraftRedirect: (draftId) => get().promotedDraftRedirects[draftId] ?? null,
2491-
getPromotedDraftIdByRef: (threadRef) => {
2492-
for (const [draftId, promotedTo] of Object.entries(get().promotedDraftRedirects)) {
2493-
if (scopedThreadRefsEqual(promotedTo, threadRef)) {
2494-
return DraftId.make(draftId);
2495-
}
2496-
}
2497-
return null;
2498-
},
2499-
retainPromotedDraftRedirectsForRoute: ({ modalPage, target }) => {
2500-
// Settings and Usage render over the draft and close back to it, so it still needs its redirect.
2501-
if (modalPage) return;
2502-
set((state) => {
2503-
const entries = Object.entries(state.promotedDraftRedirects);
2504-
const retained = entries.filter(([draftId, promotedTo]) =>
2505-
target?.kind === "draft"
2506-
? target.draftId === draftId
2507-
: target?.kind === "server" && scopedThreadRefsEqual(promotedTo, target.threadRef),
2508-
);
2509-
return retained.length === entries.length
2510-
? state
2511-
: { promotedDraftRedirects: Object.fromEntries(retained) };
2512-
});
2513-
},
25142484
getDraftThread: (threadRef) => {
25152485
if (typeof threadRef === "string") {
25162486
return get().getDraftSession(DraftId.make(threadRef));

0 commit comments

Comments
 (0)