feat(web): queue messages sent client-side while the agent is working #11673
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 12 commits into
main fromrefs/pull/11673/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/queuedMessageStore.ts line 107OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +87,21 @@87+ return entry;88+ },89+ drain: (threadKey) => {90+ const queue = get().queuesByThreadKey[threadKey];91+ if (!queue || queue.length === 0) {92+ return EMPTY_QUEUE;93+ }94+ set((state) => {95+ const queuesByThreadKey = { ...state.queuesByThreadKey };96+ delete queuesByThreadKey[threadKey];97+ return { queuesByThreadKey };98+ });99+ return queue;100+ },101+}));102+103+/** The newest finished tool call. Its id changing is the boundary a queued message goes out on. */104+export function latestCompletedToolActivityId(105+ activities: ReadonlyArray<{ readonly id: string; readonly kind: string }>,106+): string | null {107+ for (let index = activities.length - 1; index >= 0; index -= 1) {apps/web/src/components/chat/MessagesTimeline.tsx line 1507OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1427,0 +1487,21 @@1487+ <span className="min-w-0 flex-1 truncate">1488+ {row.isNext ? "Queued, sends after the next tool call" : "Queued"}1489+ </span>1490+ <Button1491+ type="button"1492+ size="xs"1493+ variant="ghost"1494+ className="h-6 rounded-md px-1.5 text-xs font-semibold text-message-foreground hover:bg-muted/55"1495+ onPointerDown={(event) => event.preventDefault()}1496+ onClick={() => ctx.onSteerQueuedMessage(queuedMessage.id)}1497+ aria-label="Steer: send this message now"1498+ >1499+ Steer1500+ </Button>1501+ <Button1502+ type="button"1503+ size="xs"1504+ variant="ghost"1505+ className="h-6 rounded-md px-1.5 text-xs hover:bg-muted/55 hover:text-message-foreground"1506+ onPointerDown={(event) => event.preventDefault()}1507+ onClick={() => ctx.onRemoveQueuedMessage(queuedMessage.id)}apps/web/src/components/ChatView.tsx lines 6805 to 6808OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -6772,2 +6788,21 @@67726788 };677367896790+ const queuedMessages = useQueuedMessages(activeThreadKey ?? "");6791+ // Puts queued messages back into the composer, e.g. after Stop or a failed6792+ // send. Prompts join with blank lines; attachments and contexts are added.6793+ const restoreQueuedMessagesToComposer = (messages: ReadonlyArray<QueuedComposerMessage>) => {6794+ if (messages.length === 0) return;6795+ const prompts = [promptRef.current, ...messages.map((message) => message.prompt)]6796+ .map((prompt) => prompt.trim())6797+ .filter((prompt) => prompt.length > 0);6798+ const nextPrompt = prompts.join("\n\n");6799+ promptRef.current = nextPrompt;6800+ setComposerDraftPrompt(composerDraftTarget, nextPrompt);6801+ const images = messages.flatMap((message) => message.images);6802+ const files = messages.flatMap((message) => message.files);6803+ if (images.length > 0) addComposerDraftImages(composerDraftTarget, images);6804+ if (files.length > 0) addComposerDraftFiles(composerDraftTarget, files);6805+ setComposerDraftTerminalContexts(composerDraftTarget, [6806+ ...composerTerminalContextsRef.current,6807+ ...messages.flatMap((message) => message.terminalContexts),6808+ ]);apps/web/src/components/chat/MessagesTimeline.tsx line 1507OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1427,0 +1487,21 @@1487+ <span className="min-w-0 flex-1 truncate">1488+ {row.isNext ? "Queued, sends after the next tool call" : "Queued"}1489+ </span>1490+ <Button1491+ type="button"1492+ size="xs"1493+ variant="ghost"1494+ className="h-6 rounded-md px-1.5 text-xs font-semibold text-message-foreground hover:bg-muted/55"1495+ onPointerDown={(event) => event.preventDefault()}1496+ onClick={() => ctx.onSteerQueuedMessage(queuedMessage.id)}1497+ aria-label="Steer: send this message now"1498+ >1499+ Steer1500+ </Button>1501+ <Button1502+ type="button"1503+ size="xs"1504+ variant="ghost"1505+ className="h-6 rounded-md px-1.5 text-xs hover:bg-muted/55 hover:text-message-foreground"1506+ onPointerDown={(event) => event.preventDefault()}1507+ onClick={() => ctx.onRemoveQueuedMessage(queuedMessage.id)}
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/ChatView.tsx line 7518Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -7089 +7188,21 @@70897188 }7189+ // A send during a running turn waits in the queue. It leaves on the next7190+ // tool boundary, when the turn ends, or when the user clicks Steer. The7191+ // provider treats a mid-turn send as a steer of the active turn, so the7192+ // dispatch below is the same either way.7193+ if (!queuedMessage && !directAnnotation && phase === "running" && activeThreadKey) {7194+ if (composerRef.current?.validateProviderInput(promptForSend) === false) {7195+ return;7196+ }7197+ useQueuedMessageStore.getState().enqueue(activeThreadKey, {7198+ prompt: promptForSend,7199+ images: [...composerImages],7200+ files: [...composerFiles],7201+ terminalContexts: [...composerTerminalContexts],7202+ previewAnnotations: [...composerPreviewAnnotations],7203+ reviewComments: [...composerReviewComments],7204+ submissionIntent,7205+ queuedAfterToolActivityId: latestCompletedToolActivityId(threadActivities),7206+ createdAt: new Date().toISOString(),7207+ });7208+ promptRef.current = "";apps/web/src/components/ChatView.tsx lines 7818 to 7818OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -7620,0 +7798,21 @@7798+ });7799+ const nextQueuedMessage = queuedMessages[0] ?? null;7800+ const latestToolActivityId = useMemo(7801+ () => (nextQueuedMessage ? latestCompletedToolActivityId(threadActivities) : null),7802+ [nextQueuedMessage, threadActivities],7803+ );7804+ // Approvals and questions block the agent; a steer landing on top of them7805+ // would answer nothing and confuse the turn, so the queue holds until the7806+ // user resolves them.7807+ const queueBlockedByPendingRequest =7808+ activePendingApproval !== null || pendingUserInputs.length > 0;7809+ useEffect(() => {7810+ if (!nextQueuedMessage || isSendBusy || queueBlockedByPendingRequest) return;7811+ if (sendInFlightRef.current) return;7812+ if (!isQueuedMessageDue({ message: nextQueuedMessage, phase, latestToolActivityId })) return;7813+ sendQueuedMessage(nextQueuedMessage);7814+ }, [isSendBusy, latestToolActivityId, nextQueuedMessage, phase, queueBlockedByPendingRequest]);7815+7816+ const onSteerQueuedMessage = (id: string) => {7817+ const message = queuedMessages.find((entry) => entry.id === id);7818+ if (!message || sendInFlightRef.current) return;
- apps/web/src/components/ChatView.tsx line 7819OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -7620,0 +7799,21 @@7799+ // after it was queued, or the turn ended. Only one leaves per boundary; the7800+ // take inside onSend re-anchors the rest.7801+ const sendQueuedMessage = useEffectEvent((message: QueuedComposerMessage) => {7802+ void onSend(undefined, message.submissionIntent, undefined, message);7803+ });7804+ const nextQueuedMessage = queuedMessages[0] ?? null;7805+ const latestToolActivityId = useMemo(7806+ () => (nextQueuedMessage ? latestCompletedToolActivityId(threadActivities) : null),7807+ [nextQueuedMessage, threadActivities],7808+ );7809+ // Approvals and questions block the agent; a steer landing on top of them7810+ // would answer nothing and confuse the turn, so the queue holds until the7811+ // user resolves them.7812+ const queueBlockedByPendingRequest =7813+ activePendingApproval !== null || pendingUserInputs.length > 0;7814+ useEffect(() => {7815+ if (!nextQueuedMessage || isSendBusy || queueBlockedByPendingRequest) return;7816+ if (sendInFlightRef.current) return;7817+ if (!isQueuedMessageDue({ message: nextQueuedMessage, phase, latestToolActivityId })) return;7818+ sendQueuedMessage(nextQueuedMessage);7819+ }, [isSendBusy, latestToolActivityId, nextQueuedMessage, phase, queueBlockedByPendingRequest]);
- apps/web/src/components/chat/MessagesTimeline.tsx lines 1462 to 1464OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1424,4 +1444,21 @@14241444 </div>14251445 );14261446 });142714471448+/** A message waiting for the running turn: a dashed user bubble with icon actions under it. */1449+function QueuedMessageTimelineRow({1450+ row,1451+}: {1452+ row: Extract<TimelineRow, { kind: "queued-message" }>;1453+}) {1454+ const ctx = use(TimelineRowCtx);1455+ const { queuedMessage } = row;1456+ const attachmentCount = queuedMessage.images.length + queuedMessage.files.length;1457+ const contextCount =1458+ queuedMessage.terminalContexts.length +1459+ queuedMessage.previewAnnotations.length +1460+ queuedMessage.reviewComments.length;1461+ const text = queuedMessage.prompt.trim();1462+ const statusLabel = row.isNext1463+ ? "Queued. Sends after the next tool call or when the turn ends."1464+ : "Queued. Sends after the messages above it.";
- apps/web/src/components/ChatView.tsx line 8151Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -7755,0 +7945,21 @@7945+ void onSend(undefined, message.submissionIntent, undefined, message);7946+ });7947+ const nextQueuedMessage = queuedMessages[0] ?? null;7948+ const latestToolActivityId = useMemo(7949+ () => (nextQueuedMessage ? latestCompletedToolActivityId(threadActivities) : null),7950+ [nextQueuedMessage, threadActivities],7951+ );7952+ // Approvals and questions block the agent; a steer landing on top of them7953+ // would answer nothing and confuse the turn, so the queue holds until the7954+ // user resolves them.7955+ const queueBlockedByPendingRequest =7956+ activePendingApproval !== null || pendingUserInputs.length > 0;7957+ // onSend bails early on transient gates (environment offline, settings not7958+ // hydrated, checkpoint rewinding, messages loading, machine not chosen) and7959+ // leaves the message queued. Re-run when any of them clear so a due message7960+ // does not wait for an unrelated phase change.7961+ const queueSendGate =7962+ activeEnvironmentUnavailable ||7963+ !clientSettingsHydrated ||7964+ isRevertingCheckpoint ||7965+ threadDetailLoading ||apps/web/src/components/ChatView.tsx line 8049Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -7687,4 +7858,9 @@76877858 }7688785976897860 if (failure !== null) {7690- if (7861+ if (queuedMessage) {7862+ setOptimisticUserMessages((existing) => {7863+ const removed = existing.filter((message) => message.id === messageIdForSend);7864+ for (const message of removed) {7865+ revokeUserMessagePreviewUrls(message);7866+ }
- apps/web/src/queuedMessageStore.ts line 90Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +63,21 @@63+64+/** In-memory only: a queued message is a live intent, not a draft worth persisting. */65+export const useQueuedMessageStore = create<QueuedMessageStoreState>()((set, get) => ({66+ queuesByThreadKey: {},67+ enqueue: (threadKey, message) => {68+ const entry: QueuedComposerMessage = { ...message, id: randomUUID() };69+ set((state) => ({70+ queuesByThreadKey: {71+ ...state.queuesByThreadKey,72+ [threadKey]: [...(state.queuesByThreadKey[threadKey] ?? EMPTY_QUEUE), entry],73+ },74+ }));75+ return entry;76+ },77+ take: (threadKey, id, toolActivityId) => {78+ const queue = get().queuesByThreadKey[threadKey];79+ const entry = queue?.find((message) => message.id === id);80+ if (!queue || !entry) {81+ return null;82+ }83+ set((state) => {
- apps/web/src/components/ChatView.tsx lines 7499 to 7528Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -7219,0 +7336,21 @@7336+ prompt: promptForSend,7337+ images: [...composerImages],7338+ files: [...composerFiles],7339+ terminalContexts: [...composerTerminalContexts],7340+ previewAnnotations: [...composerPreviewAnnotations],7341+ reviewComments: [...composerReviewComments],7342+ submissionIntent,7343+ queuedAfterToolActivityId: latestCompletedToolActivityId(threadActivities),7344+ createdAt: new Date().toISOString(),7345+ });7346+ promptRef.current = "";7347+ // Attachments move with the message; their uploads stay pending. The7348+ // refs clear now too, so a Stop before the composer's sync effect runs7349+ // does not restore the moved attachments twice.7350+ composerImagesRef.current = [];7351+ composerFilesRef.current = [];7352+ composerTerminalContextsRef.current = [];7353+ clearComposerDraftContent(composerDraftTarget);7354+ composerRef.current?.resetCursorState();7355+ return;7356+ }
t3dotgg(GitHub profile, opens in a new tab) changed the title
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.

Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
b5f7fa0· PR result:10e8eab· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.