Skip to content

Commit 7b945f3

Browse files
only21milclaude
andcommitted
fix(web): an interrupted send put back into the composer also says it was not confirmed
When nothing was typed during the send, an interrupted send went back into the composer with no notice, so it looked ready to send again even though the server may have accepted it. It now shows the same "Send not confirmed" warning, without the new-draft line or a Copy action, and it stays until dismissed. The failed-send browser test gains text and image-only rows with nothing typed meanwhile, and a new test covers the "Could not start thread" toast's Copy message action for a new thread rejected after leaving its draft. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f5c425b commit 7b945f3

2 files changed

Lines changed: 116 additions & 25 deletions

File tree

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

Lines changed: 110 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { createMemoryHistory } from "@tanstack/react-router";
22
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
33
import {
4+
CommandId,
45
ORCHESTRATION_V2_WS_METHODS,
56
OrchestrationDispatchCommandError,
7+
OrchestrationV2ThreadLaunchError,
68
ThreadId,
79
WS_METHODS,
810
} from "@t3tools/contracts";
@@ -59,6 +61,20 @@ function acceptAttachmentUploads(): Disposable {
5961
};
6062
}
6163

64+
/** Holds every call to `method` until `release`, then ends it with `end`, until disposed. */
65+
function holdRpc<Input>(method: string, end: Effect.Effect<never, Error>) {
66+
let release = () => {};
67+
const held = new Promise<void>((resolve) => {
68+
release = resolve;
69+
});
70+
const calls: Input[] = [];
71+
const answer = answerRpc(method, (input: Input) => {
72+
calls.push(input);
73+
return Effect.promise(() => held).pipe(Effect.andThen(end));
74+
});
75+
return { calls, release, [Symbol.dispose]: () => answer[Symbol.dispose]() };
76+
}
77+
6278
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
6379
vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
6480
const [actual, { fakeDriver }, Layer] = await Promise.all([
@@ -113,6 +129,7 @@ it.each([
113129
banner: "Provider is unavailable.",
114130
toast: "Message not sent",
115131
failedText: "first message that will fail",
132+
nextDraft: "next draft",
116133
},
117134
// A dropped connection interrupts the request and shows no thread error.
118135
{
@@ -121,6 +138,7 @@ it.each([
121138
banner: null,
122139
toast: "Send not confirmed",
123140
failedText: "first message that will fail",
141+
nextDraft: "next draft",
124142
},
125143
// An image alone leaves no text to copy, but the failure still needs a toast.
126144
{
@@ -129,28 +147,38 @@ it.each([
129147
banner: null,
130148
toast: "Send not confirmed",
131149
failedText: "",
150+
nextDraft: "next draft",
151+
},
152+
// With nothing typed meanwhile, the failed message goes back into the composer.
153+
{
154+
failure: "interrupted, restored",
155+
end: Effect.interrupt,
156+
banner: null,
157+
toast: "Send not confirmed",
158+
failedText: "first message that will fail",
159+
nextDraft: null,
160+
},
161+
{
162+
failure: "interrupted image, restored",
163+
end: Effect.interrupt,
164+
banner: null,
165+
toast: "Send not confirmed",
166+
failedText: "",
167+
nextDraft: null,
132168
},
133169
])(
134-
"a failed send ($failure) drops its message from the timeline and keeps the draft typed meanwhile",
135-
async ({ failure, end, banner, toast, failedText }) => {
170+
"a failed send ($failure) drops its message from the timeline and loses no draft",
171+
async ({ failure, end, banner, toast, failedText, nextDraft }) => {
136172
// Drafts persist per thread, so each case gets its own.
137-
const thread = threadWithHistory(`thread-${failure.replace(" ", "-")}-send`, 1);
173+
const thread = threadWithHistory(`thread-${failure.replace(/\W+/g, "-")}-send`, 1);
138174
using _environment = installFakeEnvironment([thread]);
139175
__setClientSettingsForTests({
140176
...DEFAULT_CLIENT_SETTINGS,
141177
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
142178
});
143-
let failDispatch = () => {};
144-
const dispatchHeld = new Promise<void>((resolve) => {
145-
failDispatch = resolve;
146-
});
147-
const dispatched: string[] = [];
148-
using _dispatch = answerRpc(
179+
using dispatch = holdRpc<{ readonly messageId: string }>(
149180
ORCHESTRATION_V2_WS_METHODS.dispatchCommand,
150-
(command: { readonly messageId: string }) => {
151-
dispatched.push(command.messageId);
152-
return Effect.promise(() => dispatchHeld).pipe(Effect.andThen(end));
153-
},
181+
end,
154182
);
155183
const router = getRouter(
156184
createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
@@ -185,17 +213,19 @@ it.each([
185213
}
186214
await userEvent.keyboard("{Enter}");
187215
await vi.waitFor(() => expect(sentMessages()).toHaveLength(2));
188-
await userEvent.keyboard("next draft");
189-
await expect.element(editor).toHaveTextContent("next draft");
190-
failDispatch();
216+
if (nextDraft) {
217+
await userEvent.keyboard(nextDraft);
218+
await expect.element(editor).toHaveTextContent(nextDraft);
219+
}
220+
dispatch.release();
191221

192222
await vi.waitFor(() => expect(sentMessages()).toEqual(onlyHistory));
193223
await expect.element(page.getByText(toast)).toBeVisible();
194224
if (banner) {
195225
await expect.element(page.getByText(banner)).toBeVisible();
196226
} else {
197227
// The server can still take an interrupted send. Its copy then shows, and nothing resends.
198-
const messageId = dispatched[0]!;
228+
const messageId = dispatch.calls[0]!.messageId;
199229
const late = runningRun(thread.id, "late-run", 2, messageId);
200230
await emitThreadEvent(thread.id, { type: "run.created", payload: late });
201231
await emitThreadEvent(thread.id, {
@@ -207,15 +237,75 @@ it.each([
207237
);
208238
expect(page.getByText("Message not sent").query()).toBeNull();
209239
}
210-
expect(dispatched).toHaveLength(1);
211-
await expect.element(editor).toHaveTextContent("next draft");
240+
expect(dispatch.calls).toHaveLength(1);
241+
// A new draft stays; otherwise the failed message is back in the composer.
242+
await expect.element(editor).toHaveTextContent(nextDraft ?? failedText);
243+
if (!nextDraft && !failedText) {
244+
await expect.element(page.getByRole("img", { name: "shot.png" })).toBeVisible();
245+
}
212246
// By text, since each timeline row has an icon button labelled the same.
213247
const copyAction = page.getByText("Copy message", { exact: true });
214-
if (!failedText) {
248+
// Only text that is no longer in the composer needs copying.
249+
if (!nextDraft || !failedText) {
215250
expect(copyAction.query()).toBeNull();
216251
return;
217252
}
218253
await copyAction.click();
219254
expect(write).toHaveBeenCalledWith(failedText);
220255
},
221256
);
257+
258+
it("a new thread rejected after you leave its draft offers the failed text from its toast", async () => {
259+
const other = threadWithHistory("thread-left-for", 1);
260+
using _environment = installFakeEnvironment([other]);
261+
__setClientSettingsForTests({
262+
...DEFAULT_CLIENT_SETTINGS,
263+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
264+
});
265+
const draftId = DraftId.make("draft-rejected-start");
266+
useComposerDraftStore
267+
.getState()
268+
.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, {
269+
threadId: ThreadId.make("thread-rejected-start"),
270+
});
271+
// A new thread starts with a launch, not a dispatch.
272+
using launch = holdRpc(
273+
ORCHESTRATION_V2_WS_METHODS.launchThread,
274+
Effect.fail(
275+
new OrchestrationV2ThreadLaunchError({
276+
commandId: CommandId.make("command-rejected-start"),
277+
projectId: PROJECT_ID,
278+
message: "Provider is unavailable.",
279+
}),
280+
),
281+
);
282+
const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
283+
await router.load();
284+
await using _app = await render(<AppRoot router={router} />, {
285+
// AppRoot brings its own router.
286+
router: false,
287+
});
288+
using write = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue(undefined);
289+
const editor = page.getByTestId("composer-editor");
290+
await expect.element(editor).toBeVisible();
291+
// The send needs the project, which arrives with the shell snapshot.
292+
await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();
293+
294+
await userEvent.click(editor);
295+
await userEvent.keyboard("first message that will fail{Enter}");
296+
await vi.waitFor(() => expect(launch.calls).toHaveLength(1));
297+
await userEvent.keyboard("later draft");
298+
await expect.element(editor).toHaveTextContent("later draft");
299+
await router.navigate({
300+
to: "/$environmentId/$threadId",
301+
params: { environmentId: ENVIRONMENT_ID, threadId: other.id },
302+
});
303+
await expect.element(page.getByText(`${other.id}: question 0`)).toBeVisible();
304+
launch.release();
305+
306+
await expect.element(page.getByText("Could not start thread")).toBeVisible();
307+
await page.getByText("Copy message", { exact: true }).click();
308+
expect(write).toHaveBeenCalledWith("first message that will fail");
309+
await page.getByText("Open draft", { exact: true }).click();
310+
await expect.element(editor).toHaveTextContent("later draft");
311+
});

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,17 +1542,18 @@ export function useComposerSend(input: ComposerSendInput) {
15421542
failureToastShown = true;
15431543
}
15441544
}
1545-
// Keeping the new draft drops the failed message, so say so even with no text to copy.
1546-
if (!restoresDraft && !failureToastShown) {
1545+
// An interrupted request may still have reached the server, so warn even when its text
1546+
// went back. Keeping a new draft drops a rejected message, so say so even with no text.
1547+
if (interrupted || (!restoresDraft && !failureToastShown)) {
15471548
toastManager.add(
15481549
stackedThreadToast({
1549-
// An interrupted request may still have reached the server.
15501550
...(interrupted
15511551
? {
15521552
type: "warning",
15531553
title: "Send not confirmed",
1554-
description:
1555-
"Check the conversation before sending again. Your new draft was kept.",
1554+
description: restoresDraft
1555+
? "Check the conversation before sending again."
1556+
: "Check the conversation before sending again. Your new draft was kept.",
15561557
}
15571558
: {
15581559
type: "error",

0 commit comments

Comments
 (0)