Skip to content

Commit cdc9998

Browse files
t3dotggclaude
andcommitted
fix(web): keep queued order through failed and overlapping sends
- A failed queued send no longer drops the wait on an earlier dispatch. - Overflow attachments restored after Stop follow the composer's settings. - A composer send lines up behind a queued message that is still leaving. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 37ab796 commit cdc9998

4 files changed

Lines changed: 85 additions & 30 deletions

File tree

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ import {
326326
import {
327327
latestCompletedToolActivityId,
328328
type QueuedComposerMessage,
329+
type QueuedMessageSendSettings,
329330
useQueuedMessages,
330331
useQueuedMessageStore,
331332
} from "../queuedMessageStore";
@@ -7248,6 +7249,22 @@ export default function ChatView(props: ChatViewProps) {
72487249
};
72497250

72507251
const queuedMessages = useQueuedMessages(activeThreadKey ?? "");
7252+
// The composer's model and modes, as a queued message keeps them for its send.
7253+
const readComposerSendSettings = (
7254+
sendCtx: ReturnType<ChatComposerHandle["getSendContext"]>,
7255+
): QueuedMessageSendSettings => ({
7256+
modelSelection: sendCtx.selectedModelSelection,
7257+
runtimeMode,
7258+
interactionMode: sendCtx.interactionMode,
7259+
promptEffort: resolvePromptInjectedEffort(
7260+
getProviderModelCapabilities(
7261+
sendCtx.selectedProviderModels,
7262+
sendCtx.selectedModel,
7263+
sendCtx.selectedProvider,
7264+
),
7265+
sendCtx.selectedPromptEffort,
7266+
),
7267+
});
72517268
// Puts queued messages back into the composer after Stop or Cancel. Prompts
72527269
// join with blank lines; attachments and contexts are added.
72537270
const restoreQueuedMessagesToComposer = (messages: ReadonlyArray<QueuedComposerMessage>) => {
@@ -7280,14 +7297,16 @@ export default function ChatView(props: ChatViewProps) {
72807297
if (restoredImages.length > 0) addComposerDraftImages(composerDraftTarget, restoredImages);
72817298
if (restoredFiles.length > 0) addComposerDraftFiles(composerDraftTarget, restoredFiles);
72827299
if (overflow.length > 0 && activeThreadKey) {
7300+
// The overflow is the rest of the restored draft, so it follows the composer.
7301+
const sendCtx = composerRef.current?.getSendContext();
72837302
useQueuedMessageStore.getState().enqueue(activeThreadKey, {
72847303
prompt: "",
72857304
images: overflow.filter((attachment) => attachment.type === "image"),
72867305
files: overflow.filter((attachment) => attachment.type === "file"),
72877306
terminalContexts: [],
72887307
previewAnnotations: [],
72897308
reviewComments: [],
7290-
sendSettings: firstMessage.sendSettings,
7309+
sendSettings: sendCtx ? readComposerSendSettings(sendCtx) : firstMessage.sendSettings,
72917310
queuedAfterToolActivityId: latestCompletedToolActivityId(threadActivities),
72927311
// Restoration is not a send. The user decides when the overflow goes.
72937312
holdUntilUserAction: true,
@@ -7664,23 +7683,24 @@ export default function ChatView(props: ChatViewProps) {
76647683
);
76657684
return;
76667685
}
7686+
// A queued message that will still leave on its own goes first, so a new
7687+
// send lines up behind it instead of overtaking it.
7688+
const queueStillSending =
7689+
activeThreadKey !== null &&
7690+
(useQueuedMessageStore.getState().queuesByThreadKey[activeThreadKey] ?? []).some(
7691+
(message) => message.sending !== undefined || !message.holdUntilUserAction,
7692+
);
76677693
if (
76687694
!directAnnotation &&
7669-
phase === "running" &&
76707695
activeThreadKey &&
7671-
(settings.followUpBehavior === "queue") !== (submissionIntent === "alternate")
7696+
(queueStillSending ||
7697+
(phase === "running" &&
7698+
(settings.followUpBehavior === "queue") !== (submissionIntent === "alternate")))
76727699
) {
7673-
const promptEffort = resolvePromptInjectedEffort(
7674-
getProviderModelCapabilities(
7675-
ctxSelectedProviderModels,
7676-
ctxSelectedModel,
7677-
ctxSelectedProvider,
7678-
),
7679-
ctxSelectedPromptEffort,
7680-
);
7700+
const sendSettings = readComposerSendSettings(sendCtx);
76817701
if (
76827702
composerRef.current?.validateProviderInput(
7683-
applyClaudePromptEffortPrefix(promptForSend, promptEffort),
7703+
applyClaudePromptEffortPrefix(promptForSend, sendSettings.promptEffort),
76847704
) === false
76857705
) {
76867706
return;
@@ -7692,12 +7712,7 @@ export default function ChatView(props: ChatViewProps) {
76927712
terminalContexts: [...composerTerminalContexts],
76937713
previewAnnotations: [...composerPreviewAnnotations],
76947714
reviewComments: [...composerReviewComments],
7695-
sendSettings: {
7696-
modelSelection: ctxSelectedModelSelection,
7697-
runtimeMode,
7698-
interactionMode: sendInteractionMode,
7699-
promptEffort,
7700-
},
7715+
sendSettings,
77017716
queuedAfterToolActivityId: latestCompletedToolActivityId(threadActivities),
77027717
createdAt: new Date().toISOString(),
77037718
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function ThreadQueueSender({ threadKey }: { threadKey: string }) {
5656
// picks it up. Hold the next message until then, as the composer does for
5757
// its own sends.
5858
const lastDispatch = useQueuedMessageStore(
59-
(state) => state.lastDispatchByThreadKey[threadKey] ?? null,
59+
(state) => state.lastDispatchByThreadKey[threadKey]?.thread ?? null,
6060
);
6161
const latestUserMessageId = thread?.messages.findLast((m) => m.role === "user")?.id ?? null;
6262
const waitingForServer =

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ describe("queuedMessageStore", () => {
102102
expect(remove("thread-a", first.id)).toBeNull();
103103
});
104104

105+
it("a failed send keeps waiting on the dispatch before it", () => {
106+
const { enqueue, beginSend, markDispatching, finishSend, failSend } =
107+
useQueuedMessageStore.getState();
108+
const earlier = createLocalDispatchSnapshot(undefined);
109+
const first = enqueue("thread-a", makeMessage("first"));
110+
const second = enqueue("thread-a", makeMessage("second"));
111+
const third = enqueue("thread-a", makeMessage("third"));
112+
const lastDispatch = () => useQueuedMessageStore.getState().lastDispatchByThreadKey["thread-a"];
113+
beginSend("thread-a", first.id, null);
114+
markDispatching("thread-a", first.id, earlier);
115+
finishSend("thread-a", first.id);
116+
117+
// Fails before its turn start went out: the first send is still the one to wait on.
118+
beginSend("thread-a", second.id, null);
119+
failSend("thread-a", second.id);
120+
expect(lastDispatch()?.thread).toBe(earlier);
121+
122+
// Fails after going out: it never reached the server, so the first still counts.
123+
beginSend("thread-a", third.id, null);
124+
markDispatching("thread-a", third.id, { ...earlier, startedAt: "later" });
125+
failSend("thread-a", third.id);
126+
expect(lastDispatch()?.thread).toBe(earlier);
127+
});
128+
105129
it("Stop takes back a preparing send but not one already dispatching", () => {
106130
const { enqueue, beginSend, markDispatching, drain, failSend } =
107131
useQueuedMessageStore.getState();

‎apps/web/src/queuedMessageStore.ts‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,22 @@ export interface QueuedComposerMessage {
5959
createdAt: string;
6060
}
6161

62+
/**
63+
* The thread as it was when its last queued turn start went out. The next
64+
* message waits until the server has moved past it, so a send that starts a
65+
* new turn and the message after it do not leave on one boundary.
66+
*/
67+
interface QueuedDispatch {
68+
/** The message that went out, or null for a dispatch restored after a failure. */
69+
messageId: string | null;
70+
thread: LocalDispatchSnapshot;
71+
/** The dispatch this one replaced. If this send fails, that one still counts. */
72+
previous: LocalDispatchSnapshot | null;
73+
}
74+
6275
interface QueuedMessageStoreState {
6376
queuesByThreadKey: Record<string, QueuedComposerMessage[]>;
64-
/**
65-
* The thread as it was when its last queued turn start went out. The next
66-
* message waits until the server has moved past it, so a send that starts
67-
* a new turn and the message after it do not leave on one boundary.
68-
*/
69-
lastDispatchByThreadKey: Record<string, LocalDispatchSnapshot>;
77+
lastDispatchByThreadKey: Record<string, QueuedDispatch>;
7078
enqueue: (threadKey: string, message: Omit<QueuedComposerMessage, "id">) => QueuedComposerMessage;
7179
/**
7280
* Marks one message as sending and returns it, or null when it is gone or
@@ -103,7 +111,7 @@ function withQueue(
103111
state: QueueState,
104112
threadKey: string,
105113
queue: QueuedComposerMessage[],
106-
lastDispatch?: LocalDispatchSnapshot | null,
114+
lastDispatch?: QueuedDispatch | null,
107115
): QueueState {
108116
const queuesByThreadKey = { ...state.queuesByThreadKey, [threadKey]: queue };
109117
const lastDispatchByThreadKey = { ...state.lastDispatchByThreadKey };
@@ -119,7 +127,7 @@ export const useQueuedMessageStore = create<QueuedMessageStoreState>()((set, get
119127
const update = (
120128
threadKey: string,
121129
queue: QueuedComposerMessage[],
122-
lastDispatch?: LocalDispatchSnapshot | null,
130+
lastDispatch?: QueuedDispatch | null,
123131
) => set((state) => withQueue(state, threadKey, queue, lastDispatch));
124132
return {
125133
queuesByThreadKey: {},
@@ -153,7 +161,11 @@ export const useQueuedMessageStore = create<QueuedMessageStoreState>()((set, get
153161
queue.map((message) =>
154162
message.id === id ? { ...message, sending: "dispatching" } : message,
155163
),
156-
thread,
164+
{
165+
messageId: id,
166+
thread,
167+
previous: get().lastDispatchByThreadKey[threadKey]?.thread ?? null,
168+
},
157169
);
158170
return true;
159171
},
@@ -170,11 +182,15 @@ export const useQueuedMessageStore = create<QueuedMessageStoreState>()((set, get
170182
const entry = queue.find((message) => message.id === id);
171183
if (!entry) return false;
172184
const { sending: _sending, ...rest } = entry;
173-
// Nothing reached the server, so there is nothing to wait for.
185+
// This send never reached the server, so only an earlier one is worth
186+
// waiting for.
187+
const dispatch = get().lastDispatchByThreadKey[threadKey];
174188
update(
175189
threadKey,
176190
[{ ...rest, holdUntilUserAction: true }, ...queue.filter((message) => message.id !== id)],
177-
null,
191+
dispatch?.messageId !== id
192+
? undefined
193+
: dispatch.previous && { messageId: null, thread: dispatch.previous, previous: null },
178194
);
179195
return true;
180196
},

0 commit comments

Comments
 (0)