TypeScript
fix(clients): queued messages can't be edited while a question is open (#25593)
Julius Marmingefe507c5a1e06
diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsxindex dbccc5d12b1..40469af6fdd 100644--- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx@@ -26,6 +26,7 @@ import type { ComposerTextPaste } from "../../native/T3ComposerEditor.types";import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";import type { HandoffEndpoints } from "@t3tools/client-runtime/handoff";+import { queuedRunEditBlockedReason } from "@t3tools/client-runtime/queued-run-edit";import {childThreadInputsTitle,type CodexFeedbackSubmission,@@ -777,6 +778,9 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {const activeUserInputRequestId = props.activePendingUserInput?.requestId ?? null;const composerSlotHidden =activeUserInputRequestId !== null || props.creationState?.kind === "failed";+ const pendingMessageEditBlocked =+ queuedRunEditBlockedReason({ openQuestionCount: activeUserInputRequestId === null ? 0 : 1 }) !==+ null;const [usageLimitsPanel, setUsageLimitsPanel] = useState<{readonly key: string;readonly threadKey: string;@@ -1167,6 +1171,7 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {};const handleEditPendingMessage = async (message: QueuedThreadMessage) => {+ if (pendingMessageEditBlocked) return;const scope = threadScope.current.signal;const edited = await settlePromise(() => editPendingThreadMessage(message));if (AsyncResult.isFailure(edited)) {@@ -1296,7 +1301,9 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {dispatchingMessageId={props.dispatchingMessageId}// A native subagent has no composer to edit a pending message in;// Cancel on the edit banner would discard it.- onEditPendingMessage={isProviderSubagent ? null : handleEditPendingMessage}+ onEditPendingMessage={+ isProviderSubagent || pendingMessageEditBlocked ? null : handleEditPendingMessage+ }contentPresentation={props.contentPresentation}agentLabel={agentLabel}threadTitle={props.selectedThread.title}diff --git a/apps/mobile/src/features/threads/ThreadQueueControl.tsx b/apps/mobile/src/features/threads/ThreadQueueControl.tsxindex f2ce81db0b6..3f9ca611312 100644--- a/apps/mobile/src/features/threads/ThreadQueueControl.tsx+++ b/apps/mobile/src/features/threads/ThreadQueueControl.tsx@@ -1,6 +1,9 @@import { type StaticScreenProps, useNavigation } from "@react-navigation/native";import { useAtomValue } from "@effect/atom-react";-import { makeQueuedRunsAtom } from "@t3tools/client-runtime/queued-run-edit";+import {+ makeQueuedRunsAtom,+ queuedRunEditBlockedReason,+} from "@t3tools/client-runtime/queued-run-edit";import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";import { Atom } from "effect/reactivity";import { Image } from "expo-image";@@ -75,6 +78,10 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {const workflow = useThreadQueueWorkflow(target);const threadKey = scopedThreadKey(target.environmentId, target.threadId);const editing = useQueuedRunEdit(threadKey);+ const pendingRequests = useAtomValue(environmentThreadDetails.pendingRequestsAtom(target));+ const editBlockedReason = queuedRunEditBlockedReason({+ openQuestionCount: pendingRequests?.userInputs.length ?? 0,+ });const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun, "reorder queued message");const promote = useAtomCommand(threadEnvironment.promoteQueuedRun, "promote queued message");const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun, "remove queued message");@@ -127,6 +134,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {return;}if (action === "edit") {+ if (editBlockedReason !== null) return;const entry = queuedRuns[index]!;void Haptics.selectionAsync();beginQueuedRunEdit(@@ -212,6 +220,9 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {/></View>) : null}+ {editBlockedReason !== null && queuedRuns.length > 0 ? (+ <Text className="py-3 text-sm text-foreground-muted">{editBlockedReason}</Text>+ ) : null}{queuedRuns.length === 0 ? (<Text className="pt-6 text-center text-sm text-foreground-muted">No messages waiting in this queue.@@ -231,6 +242,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {canPromoteToSteer: workflow?.canPromoteToSteer ?? false,canSendNow: workflow?.canSendNow ?? false,canReorder: workflow?.canReorder ?? false,+ editBlocked: editBlockedReason !== null,index,isEditing: editing?.runId === run.id,isGuestMessage: context?.guest !== undefined,@@ -372,7 +384,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {accessibilityHint={controls.canEdit? "Opens this message in the composer for editing"- : undefined+ : (editBlockedReason ?? undefined)}disabled={!controls.canEdit}onPress={() => void act(run.id, "edit")}diff --git a/apps/mobile/src/features/threads/threadQueueControlPresentation.ts b/apps/mobile/src/features/threads/threadQueueControlPresentation.tsindex 9ffffd98476..33c313f1d39 100644--- a/apps/mobile/src/features/threads/threadQueueControlPresentation.ts+++ b/apps/mobile/src/features/threads/threadQueueControlPresentation.ts@@ -19,6 +19,8 @@ export function resolveThreadQueueRowControls(input: {readonly canPromoteToSteer: boolean;readonly canSendNow: boolean;readonly canReorder: boolean;+ /** Something else holds the composer, such as an open question. */+ readonly editBlocked?: boolean;readonly index: number;readonly isEditing?: boolean;/** A guest's message keeps the guest's words; the owner can cancel it, not rewrite it. */@@ -33,7 +35,8 @@ export function resolveThreadQueueRowControls(input: {canDismiss: !input.busy,// Re-opening the row already in the composer would reload it and throw// away whatever has been typed since.- canEdit: mutationEnabled && !isEditing && input.isGuestMessage !== true,+ canEdit:+ mutationEnabled && !isEditing && input.isGuestMessage !== true && input.editBlocked !== true,canMoveDown: mutationEnabled && input.canReorder && input.index < input.queuedCount - 1,canMoveUp: mutationEnabled && input.canReorder && input.index > 0,canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 72ad50679d6..6d608595848 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -1,4 +1,5 @@import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";+import { queuedRunEditBlockedReason } from "@t3tools/client-runtime/queued-run-edit";import { ScrollToEndButton, WorkspaceFileDropOverlay } from "./chat/TimelineOverlays";import { usePasteToFocusComposer } from "./chat/usePasteToFocusComposer";import { announcedRun, isDetailBehindShell } from "./chat/threadActivityAnnouncement";@@ -2924,6 +2925,9 @@ export default function ChatView(props: ChatViewProps) {threadId={activeThread.id}optimisticMessages={optimisticUserMessages}editingRunId={queuedRunEdit.editingRunId}+ editBlockedReason={queuedRunEditBlockedReason({+ openQuestionCount: pendingUserInputs.length,+ })}onEditQueuedRun={queuedRunEdit.beginEdit}onCancelEdit={queuedRunEdit.cancelEdit}/>diff --git a/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx b/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsxindex 3c7ca7ccea6..c4d51e6a889 100644--- a/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx+++ b/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx@@ -43,6 +43,7 @@ const baseProps = {optimisticMessages: [],threadId: "thread:test" as never,editingRunId: null,+ editBlockedReason: null,onEditQueuedRun: () => undefined,onCancelEdit: () => undefined,} satisfies ComponentProps<typeof QueuedRunsControl>;diff --git a/apps/web/src/components/chat/QueuedRunsControl.tsx b/apps/web/src/components/chat/QueuedRunsControl.tsxindex e6ce400ffd6..fdcba40c6ac 100644--- a/apps/web/src/components/chat/QueuedRunsControl.tsx+++ b/apps/web/src/components/chat/QueuedRunsControl.tsx@@ -74,6 +74,8 @@ export function QueuedRunsControl({Pick<ChatMessage, "id" | "inputIntent" | "text" | "attachments">>;readonly editingRunId: RunId | null;+ /** Set while something else holds the composer, such as an open question. */+ readonly editBlockedReason: string | null;readonly onEditQueuedRun: (request: EditQueuedRunRequest) => void;readonly onCancelEdit: () => void;}) {@@ -207,6 +209,7 @@ export function QueuedRunsControl({!latest ||latest.context?.guest !== undefined ||props.editingRunId !== null ||+ props.editBlockedReason !== null ||busyRunId !== null) {return false;@@ -413,32 +416,35 @@ export function QueuedRunsControl({) : (<><Tooltip>- <TooltipTrigger- render={- <Button- size="icon-xs"- variant="ghost-muted"- aria-label="Edit queued message"- disabled={- item.runId === null || !item.editable || busyRunId !== null+ {/* The span keeps the tooltip, and its reason, on a disabled button. */}+ <TooltipTrigger render={<span className="flex shrink-0" />}>+ <Button+ size="icon-xs"+ variant="ghost-muted"+ aria-label="Edit queued message"+ disabled={+ item.runId === null ||+ !item.editable ||+ busyRunId !== null ||+ props.editBlockedReason !== null+ }+ onClick={() => {+ if (item.runId !== null && item.messageId !== null) {+ props.onEditQueuedRun({+ runId: item.runId,+ messageId: item.messageId,+ text: item.text,+ attachments: item.attachments,+ });}- onClick={() => {- if (item.runId !== null && item.messageId !== null) {- props.onEditQueuedRun({- runId: item.runId,- messageId: item.messageId,- text: item.text,- attachments: item.attachments,- });- }- }}- />- }- >- <PencilIcon />+ }}+ >+ <PencilIcon />+ </Button></TooltipTrigger><TooltipPopup>- {`Edit in the composer${item.serverIndex === queued.length - 1 && props.editShortcutLabel ? ` (${props.editShortcutLabel})` : ""}`}+ {props.editBlockedReason ??+ `Edit in the composer${item.serverIndex === queued.length - 1 && props.editShortcutLabel ? ` (${props.editShortcutLabel})` : ""}`}</TooltipPopup></Tooltip><Tooltip>diff --git a/packages/client-runtime/src/queuedRunEdit.test.ts b/packages/client-runtime/src/queuedRunEdit.test.tsindex 6ecd2bc59cd..6d71f3ec11f 100644--- a/packages/client-runtime/src/queuedRunEdit.test.ts+++ b/packages/client-runtime/src/queuedRunEdit.test.ts@@ -5,6 +5,7 @@ import { describe, expect, it } from "vite-plus/test";import {makeQueuedRunEditStore,makeQueuedRunsAtom,+ queuedRunEditBlockedReason,resolveQueuedEditExit,type QueuedRunStatus,} from "./queuedRunEdit.ts";@@ -40,6 +41,15 @@ describe("resolveQueuedEditExit", () => {});});+describe("queuedRunEditBlockedReason", () => {+ it("blocks edits only while a question holds the composer", () => {+ expect(queuedRunEditBlockedReason({ openQuestionCount: 0 })).toBeNull();+ expect(queuedRunEditBlockedReason({ openQuestionCount: 1 })).toBe(+ "Answer the question to edit queued messages",+ );+ });+});+describe("makeQueuedRunEditStore", () => {it("fires once when the edited run leaves the queue, with the latest edit", () => {const { registry, runs, store, exits, begin } = setup();diff --git a/packages/client-runtime/src/queuedRunEdit.ts b/packages/client-runtime/src/queuedRunEdit.tsindex d366a0d1f39..f23c673b6dc 100644--- a/packages/client-runtime/src/queuedRunEdit.ts+++ b/packages/client-runtime/src/queuedRunEdit.ts@@ -61,6 +61,17 @@ export function resolveQueuedEditExit(input: {return input.threadDraftEmpty ? "kept" : "discarded";}+/**+ * Why a queued message can't be opened in the composer now, or null when it+ * can. An open question takes the composer over for its answer, so an edit+ * begun then would happen out of sight and could be sent before it is seen.+ */+export function queuedRunEditBlockedReason(input: {+ readonly openQuestionCount: number;+}): string | null {+ return input.openQuestionCount > 0 ? "Answer the question to edit queued messages" : null;+}+interface ExitWatch<E> {readonly runs: QueuedRunsAtom;readonly runId: RunId;