Skip to content

Commit 13d80d2

Browse files
PR Batch Testerclaude
andcommitted
fix(web): restore a cancelled launch's kept message after its draft is sent
A cancelled launch whose message did not fit its composer kept a restore that wrote to the draft id. Sending the newer draft promoted it and moved its composer to the thread, so restoring later wrote to an orphaned draft key, reported success and dropped the row. Restore to the reopened thread's ref, which resolves to the draft before promotion and to the thread after it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ac66b35 commit 13d80d2

3 files changed

Lines changed: 88 additions & 25 deletions

File tree

‎apps/web/src/components/chat/chatViewSend.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import { newThreadId } from "~/lib/utils";
2626
import {
2727
type ComposerFileAttachment,
2828
type ComposerImageAttachment,
29+
type DraftThreadState,
2930
composerDraftHasUserContent,
31+
restoreFailedDraftThread,
3032
useComposerDraftStore,
3133
DraftId,
3234
} from "../../composerDraftStore";
@@ -252,6 +254,19 @@ export async function rewindToMessage(input: {
252254
}
253255
}
254256

257+
/**
258+
* Reopens a cancelled launch's draft as `threadRef` and returns where its unsent message
259+
* restores. The thread ref still resolves after that draft is sent and promoted.
260+
*/
261+
export function reopenUnsentDraftThread(
262+
draftId: DraftId,
263+
draftThread: DraftThreadState,
264+
threadRef: ScopedThreadRef,
265+
): ScopedThreadRef {
266+
restoreFailedDraftThread(draftId, draftThread, threadRef.threadId);
267+
return threadRef;
268+
}
269+
255270
/**
256271
* Writes an unsent message's composer contents back into its draft, after any newer draft
257272
* started meanwhile. Images are fresh copies, so their previews outlive the optimistic row.

‎apps/web/src/components/chat/useComposerSend.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ import {
7171
clearBackgroundDraftSubmissionByRef,
7272
finalizePromotedDraftThreadByRef,
7373
markPromotedDraftThreadByRef,
74-
restoreFailedDraftThread,
7574
composerDraftHasUserContent,
7675
type ComposerFileAttachment,
7776
type ComposerImageAttachment,
@@ -123,6 +122,7 @@ import {
123122
type MultipleModelTarget,
124123
resolveMultipleModelTargets,
125124
prepareTurnAttachments,
125+
reopenUnsentDraftThread,
126126
restoreComposerDraft,
127127
} from "./chatViewSend";
128128
import type {
@@ -1258,12 +1258,17 @@ export function useComposerSend(input: ComposerSendInput) {
12581258
// `failure` is null when the user cancelled a send that was waiting on a reconnect.
12591259
const restoreUnsentDraft = (failure: TurnDeliveryFailure | null) => {
12601260
let restoredThreadId = threadIdForSend;
1261+
let restoreTarget = composerDraftTarget;
12611262
if (isLocalDraftThread && draftId && draftThread) {
12621263
// A cancelled launch may still have committed, so its draft must not reuse the thread id.
12631264
if (failure === null || wasBootstrapThreadDeleted(squashAtomCommandFailure(failure))) {
12641265
restoredThreadId = newThreadId();
12651266
}
1266-
restoreFailedDraftThread(draftId, draftThread, restoredThreadId);
1267+
restoreTarget = reopenUnsentDraftThread(
1268+
draftId,
1269+
draftThread,
1270+
scopeThreadRef(environmentId, restoredThreadId),
1271+
);
12671272
clearBackgroundDraftSubmissionByRef(scopeThreadRef(environmentId, threadIdForSend));
12681273
}
12691274
setOptimisticUserMessages((existing) => {
@@ -1274,8 +1279,7 @@ export function useComposerSend(input: ComposerSendInput) {
12741279
const next = existing.filter((message) => message.id !== messageIdForSend);
12751280
return next.length === existing.length ? existing : next;
12761281
});
1277-
const restore = () =>
1278-
restoreUnsentComposer(composerDraftTarget, routeThreadKey, sendSnapshot);
1282+
const restore = () => restoreUnsentComposer(restoreTarget, routeThreadKey, sendSnapshot);
12791283
if (!restore()) {
12801284
unsentMessages.keep({
12811285
environmentId,

‎apps/web/src/state/unsentMessages.test.ts‎

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
12
import {
23
EnvironmentId,
34
MessageId,
45
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
6+
ProjectId,
57
ThreadId,
68
} from "@t3tools/contracts";
79
import { expect, it } from "vite-plus/test";
810

9-
import { useComposerDraftStore } from "../composerDraftStore";
11+
import {
12+
DraftId,
13+
finalizePromotedDraftThreadByRef,
14+
markPromotedDraftThreadByRef,
15+
useComposerDraftStore,
16+
} from "../composerDraftStore";
1017
import { snapshotComposerSend } from "../components/ChatView.logic";
11-
import { restoreComposerDraft } from "../components/chat/chatViewSend";
18+
import { reopenUnsentDraftThread, restoreComposerDraft } from "../components/chat/chatViewSend";
1219
import type { ChatMessage } from "../types";
1320
import { unsentMessages } from "./unsentMessages";
1421

@@ -29,17 +36,14 @@ function image(id: string) {
2936
};
3037
}
3138

32-
function keepUnsentBesideFullDraft() {
33-
useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
34-
const store = useComposerDraftStore.getState();
35-
store.setPrompt(target, "Newer draft");
36-
store.addImages(
37-
target,
38-
Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS }, (_, index) =>
39-
image(`newer-${index}`),
40-
),
39+
function newerImages() {
40+
return Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS }, (_, index) =>
41+
image(`newer-${index}`),
4142
);
42-
const snapshot = snapshotComposerSend({
43+
}
44+
45+
function unsentSnapshot() {
46+
return snapshotComposerSend({
4347
prompt: "Unsent message",
4448
images: [image("unsent")],
4549
files: [],
@@ -49,15 +53,24 @@ function keepUnsentBesideFullDraft() {
4953
reviewComments: [],
5054
threadContexts: [],
5155
});
52-
const display: ChatMessage = {
53-
id: messageId,
54-
role: "user",
55-
text: "Unsent message",
56-
runId: null,
57-
createdAt: "2026-10-25T00:00:00.000Z",
58-
updatedAt: "2026-10-25T00:00:00.000Z",
59-
streaming: false,
60-
};
56+
}
57+
58+
const display: ChatMessage = {
59+
id: messageId,
60+
role: "user",
61+
text: "Unsent message",
62+
runId: null,
63+
createdAt: "2026-10-25T00:00:00.000Z",
64+
updatedAt: "2026-10-25T00:00:00.000Z",
65+
streaming: false,
66+
};
67+
68+
function keepUnsentBesideFullDraft() {
69+
useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
70+
const store = useComposerDraftStore.getState();
71+
store.setPrompt(target, "Newer draft");
72+
store.addImages(target, newerImages());
73+
const snapshot = unsentSnapshot();
6174
const restore = () => restoreComposerDraft(target, snapshot);
6275
expect(restore()).toBe(false);
6376
unsentMessages.keep({ environmentId, threadId, display, restore });
@@ -88,3 +101,34 @@ it("drops a kept message only when it is discarded", () => {
88101
expect(unsentMessages.getSnapshot()).toEqual([]);
89102
expect(store.getComposerDraft(target)?.prompt).toBe("Newer draft");
90103
});
104+
105+
it("restores a cancelled launch into its thread after the newer draft there is sent", () => {
106+
useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
107+
const store = useComposerDraftStore.getState();
108+
const draftId = DraftId.make("launch-draft");
109+
store.setProjectDraftThreadId(scopeProjectRef(environmentId, ProjectId.make("p")), draftId, {
110+
threadId: ThreadId.make("launched"),
111+
});
112+
const launched = store.getDraftSession(draftId)!;
113+
markPromotedDraftThreadByRef(scopeThreadRef(environmentId, launched.threadId));
114+
store.setPrompt(draftId, "Newer draft");
115+
store.addImages(draftId, newerImages());
116+
117+
const reopenedRef = scopeThreadRef(environmentId, ThreadId.make("reopened"));
118+
const restoreTarget = reopenUnsentDraftThread(draftId, launched, reopenedRef);
119+
const snapshot = unsentSnapshot();
120+
const restore = () => restoreComposerDraft(restoreTarget, snapshot);
121+
expect(restore()).toBe(false);
122+
unsentMessages.keep({ environmentId, threadId: reopenedRef.threadId, display, restore });
123+
124+
markPromotedDraftThreadByRef(reopenedRef);
125+
store.clearComposerContent(draftId);
126+
finalizePromotedDraftThreadByRef(reopenedRef);
127+
expect(store.getDraftSession(draftId)).toBeNull();
128+
129+
expect(unsentMessages.restore(environmentId, messageId)).toBe(true);
130+
expect(unsentMessages.getSnapshot()).toEqual([]);
131+
const draft = store.getComposerDraft(reopenedRef);
132+
expect(draft?.prompt).toBe("Unsent message");
133+
expect(draft?.images.map((attachment) => attachment.id)).toEqual(["unsent"]);
134+
});

0 commit comments

Comments
 (0)