The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): sent videos no longer flash black and queued attachments keep uploading (#37681)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 16, 2026 at 09:31 UTC
4860a730695762331cc482253ba60beda7875c40
11 files changed
+407
-82
apps/mobile/src/features/threads/ThreadFeed.tsx
+93
-58
@@ -143,7 +143,10 @@ import { useFilePreviewGallery, FilePreviewGallery } from "../../components/ui/F
143
import { assetImageSource } from "../../lib/assetImageSource";
144
import { filePreviewSourceIdentifier } from "../../lib/filePreviewGallery";
145
import { composerImageGallery } from "../../lib/composerImageGallery";
146
-import { isFileBackedComposerAttachment } from "../../lib/composerImages";
146
+import {
147
+ isFileBackedComposerAttachment,
148
+ type FileBackedComposerAttachment,
149
+} from "../../lib/composerImages";
150
import { useLocalAttachmentPreview } from "../../lib/localAttachmentPreview";
151
import {
152
rememberSentImage,
@@ -180,7 +183,11 @@ import {
183
import { AppText as Text } from "../../components/ui/AppText";
184
import { VideoPreviewModal, type VideoPreviewSource } from "../../components/ui/VideoPreviewModal";
185
import { markdownFileEmbedLabel, resolveMarkdownMediaPreview } from "../../lib/markdownMedia";
183
-import { attachmentVideoPreviewSource } from "../../lib/videoPreviewSource";
186
+import {
187
+ attachmentVideoPreviewSource,
188
+ mediaVideoThumbnailKey,
189
+ type MediaVideoPreviewSource,
190
+} from "../../lib/videoPreviewSource";
191
import { CopyTextButton } from "./CopyTextButton";
192
import { getReviewSectionIdForCheckpoint } from "../review/reviewModel";
193
import { setReviewSelectedSectionId } from "../review/reviewState";
@@ -672,7 +679,6 @@ function MessageAttachmentFile(props: {
679
readonly attachment: ChatFileAttachment;
680
readonly onPressPreview: (source: FilePreviewSource) => void;
681
readonly onPlaybackStart: () => void;
675
- readonly standaloneMedia?: boolean;
682
readonly standaloneFile?: boolean;
683
readonly available?: boolean;
684
}) {
@@ -809,14 +815,10 @@ function MessageAttachmentFile(props: {
815
816
if (videoType !== null) {
817
return (
812
- <View
813
- className={props.standaloneMedia ? undefined : "my-1"}
814
- style={{ width: props.standaloneMedia ? 320 : 224, maxWidth: "100%" }}
815
- >
818
+ <View className="my-1" style={{ width: 224, maxWidth: "100%" }}>
819
<ThreadMarkdownVideo
820
source={attachmentVideoPreviewSource(props.environmentId, attachment, sourceIdentifier)}
821
onPlaybackStart={props.onPlaybackStart}
819
- borderRadius={props.standaloneMedia ? 20 : undefined}
822
/>
823
</View>
824
);
@@ -907,7 +909,6 @@ function UserMessageMedia(props: {
909
...props.pendingAttachments.map((attachment, index) => {
910
const image = isImageAttachment(attachment);
911
return {
910
- id: attachment.id,
912
image,
913
content:
914
image && attachment.uploadedAttachmentId ? (
@@ -921,30 +922,31 @@ function UserMessageMedia(props: {
922
onImageLoad={(loaded) => rememberSentImage(props.messageId, index, loaded)}
923
onPressPreview={props.onPressPreview}
924
/>
924
- ) : !image && attachment.uploadedAttachmentId ? (
925
- <ThreadMarkdownVideo
926
- source={attachmentVideoPreviewSource(props.environmentId, {
927
- ...attachment,
928
- type: "file",
929
- id: attachment.uploadedAttachmentId,
930
- })}
925
+ ) : !image ? (
926
+ <UserMessageVideo
927
+ environmentId={props.environmentId}
928
+ messageId={props.messageId}
929
+ index={index}
930
+ local={isFileBackedComposerAttachment(attachment) ? attachment : null}
931
+ uploaded={
932
+ attachment.uploadedAttachmentId === undefined
933
+ ? null
934
+ : { ...attachment, type: "file", id: attachment.uploadedAttachmentId }
935
+ }
936
onPlaybackStart={props.onPlaybackStart}
932
- borderRadius={20}
937
/>
938
) : (
935
- <PendingMessageMedia
939
+ <PendingMessageImage
940
attachment={attachment}
941
{...imageProps(index)}
942
{...handoffProps(sentImageHandoff(props.messageId, index, attachment.id))}
943
onImageLoad={(image) => rememberSentImage(props.messageId, index, image)}
944
onPressPreview={props.onPressPreview}
941
- onPlaybackStart={props.onPlaybackStart}
945
/>
946
),
947
};
948
}),
949
...props.attachments.map((attachment, index) => ({
947
- id: attachment.id,
950
image: isImageAttachment(attachment),
951
content: isImageAttachment(attachment) ? (
952
<MessageAttachmentImage
@@ -957,12 +959,13 @@ function UserMessageMedia(props: {
959
onPressPreview={props.onPressPreview}
960
/>
961
) : isFileAttachment(attachment) ? (
960
- <MessageAttachmentFile
962
+ <UserMessageVideo
963
environmentId={props.environmentId}
962
- attachment={attachment}
963
- onPressPreview={props.onPressPreview}
964
+ messageId={props.messageId}
965
+ index={index}
966
+ local={null}
967
+ uploaded={attachment}
968
onPlaybackStart={props.onPlaybackStart}
965
- standaloneMedia
969
/>
970
) : null,
971
})),
@@ -986,7 +989,8 @@ function UserMessageMedia(props: {
989
media
990
.filter((_, index) => index % 2 === 0)
991
.map((item, index) => (
989
- <View key={item.id} className="flex-row" style={{ gap: StyleSheet.hairlineWidth }}>
992
+ // oxlint-disable-next-line react/no-array-index-key -- delivery re-ids attachments; position is the stable identity
993
+ <View key={index} className="flex-row" style={{ gap: StyleSheet.hairlineWidth }}>
994
<View className="min-w-0 flex-1">{item.content}</View>
995
{media[index * 2 + 1] ? (
996
<View className="min-w-0 flex-1">{media[index * 2 + 1]!.content}</View>
@@ -998,9 +1002,10 @@ function UserMessageMedia(props: {
1002
</View>
1003
) : (
1004
<View className="items-end gap-2">
1001
- {media.map((item) => (
1005
+ {media.map((item, index) => (
1006
<View
1003
- key={item.id}
1007
+ // oxlint-disable-next-line react/no-array-index-key -- delivery re-ids attachments; a new key would remount the media
1008
+ key={index}
1009
className="overflow-hidden rounded-[20px]"
1010
style={{ width: item.image ? undefined : 320, maxWidth: "100%" }}
1011
>
@@ -1015,7 +1020,7 @@ function UserMessageMedia(props: {
1020
);
1021
}
1022
1018
-function PendingMessageMedia(props: {
1023
+function PendingMessageImage(props: {
1024
readonly attachment: QueuedThreadMessage["attachments"][number];
1025
readonly className: string;
1026
readonly maxWidth?: number;
@@ -1023,36 +1028,70 @@ function PendingMessageMedia(props: {
1028
readonly handoff?: SentImageHandoff;
1029
readonly onImageLoad?: (image: SentImageHandoff) => void;
1030
readonly onPressPreview: (source: FilePreviewSource) => void;
1026
- readonly onPlaybackStart: () => void;
1031
}) {
1032
const withGallery = useFilePreviewGallery();
1033
const { attachment, handoff } = props;
1034
const fileBacked = isFileBackedComposerAttachment(attachment);
1035
const preview = useLocalAttachmentPreview(fileBacked ? attachment : null);
1032
- if (isImageAttachment(attachment)) {
1033
- // The composer already showed this image; reuse its pixels and shape instead of a blank box.
1034
- const uri =
1035
- handoff?.uri ?? (fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri));
1036
- return (
1037
- <MessageImage
1038
- key={attachment.id}
1039
- source={uri == null ? undefined : { uri }}
1040
- className={props.className}
1041
- maxWidth={props.maxWidth}
1042
- height={props.height}
1043
- accessibilityLabel={`Open ${attachment.name}`}
1044
- {...handoffProps(handoff)}
1045
- onLoad={(size) => {
1046
- if (uri) props.onImageLoad?.({ uri, aspectRatio: size.width / size.height });
1047
- }}
1048
- onPress={() => {
1049
- const source = composerImageGallery([attachment])[0];
1050
- if (source) props.onPressPreview(withGallery(source));
1051
- }}
1052
- />
1053
- );
1036
+ // The composer already showed this image; reuse its pixels and shape instead of a blank box.
1037
+ const uri =
1038
+ handoff?.uri ?? (fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri));
1039
+ return (
1040
+ <MessageImage
1041
+ key={attachment.id}
1042
+ source={uri == null ? undefined : { uri }}
1043
+ className={props.className}
1044
+ maxWidth={props.maxWidth}
1045
+ height={props.height}
1046
+ accessibilityLabel={`Open ${attachment.name}`}
1047
+ {...handoffProps(handoff)}
1048
+ onLoad={(size) => {
1049
+ if (uri) props.onImageLoad?.({ uri, aspectRatio: size.width / size.height });
1050
+ }}
1051
+ onPress={() => {
1052
+ const source = composerImageGallery([attachment])[0];
1053
+ if (source) props.onPressPreview(withGallery(source));
1054
+ }}
1055
+ />
1056
+ );
1057
+}
1058
+
1059
+/**
1060
+ * A sent video from queueing through delivery. Every stage renders this one player under a
1061
+ * thumbnail key of message and position, so swapping the local file for the server copy keeps
1062
+ * the poster on screen instead of remounting to black and extracting a frame again.
1063
+ */
1064
+function UserMessageVideo(props: {
1065
+ readonly environmentId: EnvironmentId;
1066
+ readonly messageId: string;
1067
+ readonly index: number;
1068
+ readonly local: FileBackedComposerAttachment | null;
1069
+ readonly uploaded: ChatFileAttachment | null;
1070
+ readonly onPlaybackStart: () => void;
1071
+}) {
1072
+ const sourceIdentifier = useId();
1073
+ const preview = useLocalAttachmentPreview(props.local);
1074
+ const current: MediaVideoPreviewSource | null =
1075
+ props.local !== null && preview.uri !== null
1076
+ ? {
1077
+ type: "media",
1078
+ name: props.local.name,
1079
+ mimeType: props.local.mimeType,
1080
+ uri: preview.uri,
1081
+ }
1082
+ : props.uploaded !== null
1083
+ ? attachmentVideoPreviewSource(props.environmentId, props.uploaded, sourceIdentifier)
1084
+ : null;
1085
+ // Each copy of a message reopens its local file; keep the last source while the next one loads.
1086
+ const [shown, setShown] = useState(current);
1087
+ if (
1088
+ current !== null &&
1089
+ (shown === null || mediaVideoThumbnailKey(current) !== mediaVideoThumbnailKey(shown))
1090
+ ) {
1091
+ setShown(current);
1092
}
1055
- if (preview.uri === null) {
1093
+ const source = current ?? shown;
1094
+ if (source === null) {
1095
return (
1096
<View className="aspect-video items-center justify-center bg-black">
1097
{preview.error ? (
@@ -1065,12 +1104,8 @@ function PendingMessageMedia(props: {
1104
}
1105
return (
1106
<ThreadMarkdownVideo
1068
- source={{
1069
- type: "media",
1070
- name: attachment.name,
1071
- mimeType: attachment.mimeType,
1072
- uri: preview.uri,
1073
- }}
1107
+ source={source}
1108
+ thumbnailKey={JSON.stringify(["user-message-video", props.messageId, props.index])}
1109
onPlaybackStart={props.onPlaybackStart}
1110
borderRadius={20}
1111
/>
apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx
+3
-1
@@ -14,6 +14,8 @@ export const ThreadMediaRetainContext = createContext<(retained: boolean) => voi
14
15
export function ThreadMarkdownVideo(props: {
16
readonly source: MediaVideoPreviewSource;
17
+ /** Keeps the player and its poster across source changes that show the same video. */
18
+ readonly thumbnailKey?: string;
19
readonly thumbnailVisible?: boolean;
20
readonly onPlaybackStart?: () => void;
21
readonly borderRadius?: number;
@@ -21,7 +23,7 @@ export function ThreadMarkdownVideo(props: {
23
const { source } = props;
24
const feedVisible = useContext(ThreadMediaVisibleContext);
25
const visible = props.thumbnailVisible ?? feedVisible;
24
- const thumbnailKey = mediaVideoThumbnailKey(source);
26
+ const thumbnailKey = props.thumbnailKey ?? mediaVideoThumbnailKey(source);
27
const asset = useAssetUrlState(
28
"environmentId" in source ? source.environmentId : null,
29
"resource" in source ? source.resource : null,
apps/mobile/src/features/threads/pending-thread-feed.test.ts
+31
@@ -9,6 +9,7 @@ import {
9
} from "@t3tools/contracts";
10
import type { QueuedThreadMessage } from "../../state/thread-outbox-model";
11
import {
12
+ appendAcceptedThreadMessages,
13
appendPendingThreadMessages,
14
createThreadFeedGrouping,
15
isCompactionCommandFeedEntry,
@@ -138,6 +139,36 @@ describe("pending timeline messages", () => {
139
expect(result[0]?.acknowledged).toBe(false);
140
});
141
142
+ it("keeps a new thread's first message attachments after delivery until the server copy loads", () => {
143
+ const video = {
144
+ id: "video-1",
145
+ type: "file" as const,
146
+ name: "clip.mp4",
147
+ mimeType: "video/mp4",
148
+ sizeBytes: 3,
149
+ fileUri: "file:///documents/clip.mp4",
150
+ };
151
+ const creation = {
152
+ ...pending("creation"),
153
+ attachments: [video],
154
+ creation: {
155
+ projectId: ProjectId.make("project"),
156
+ workspaceMode: "worktree" as const,
157
+ branch: "main",
158
+ worktreePath: null,
159
+ },
160
+ };
161
+ const synthetic = {
162
+ ...appendPendingThreadMessages([], [], [creation])[0]!,
163
+ pendingMessage: undefined,
164
+ };
165
+ // Delivered and caught up by sequence, but the server's copy is not projected yet.
166
+ const result = appendAcceptedThreadMessages([synthetic], [], creation);
167
+ expect(result).toHaveLength(1);
168
+ expect(result[0]?.pendingMessage?.attachments).toEqual([video]);
169
+ expect(result[0]?.acknowledged).toBe(true);
170
+ });
171
+
172
it("retains context records while a message is waiting for delivery", () => {
173
const record = {
174
version: 1 as const,
apps/mobile/src/features/threads/pending-thread-feed.ts
+17
@@ -158,6 +158,23 @@ export function appendPendingThreadMessages(
158
];
159
}
160
161
+/**
162
+ * Messages the server accepted but has not projected yet. A new thread's first message stays
163
+ * anchored until the server's copy loads, and keeps its local attachments so its media does
164
+ * not drop out of the feed in between.
165
+ */
166
+export function appendAcceptedThreadMessages(
167
+ feed: ReadonlyArray<ThreadFeedEntry>,
168
+ acknowledged: ReadonlyArray<QueuedThreadMessage>,
169
+ creation: QueuedThreadMessage | null,
170
+): ReadonlyArray<PendingThreadFeedEntry> {
171
+ const accepted = creation === null ? acknowledged : [...acknowledged, creation];
172
+ if (accepted.length === 0) return feed;
173
+ return appendPendingThreadMessages(feed, feed, accepted).map((entry) =>
174
+ entry.pendingMessage ? { ...entry, acknowledged: true } : entry,
175
+ );
176
+}
177
+
178
export function isCompactionCommandFeedEntry(entry: PendingThreadFeedEntry): boolean {
179
return (
180
entry.type === "message" &&
apps/mobile/src/lib/composerAttachmentUploadQueue.ts
+24
@@ -105,6 +105,30 @@ export function composerAttachmentsStillUploading(input: {
105
});
106
}
107
108
+/** Changes when an upload starts or settles, not on its progress ticks. */
109
+export function uploadingComposerAttachmentKeys(
110
+ states: Readonly<Record<string, ComposerAttachmentUploadState>>,
111
+): string {
112
+ return Object.keys(states)
113
+ .filter((key) => states[key]?.status === "uploading")
114
+ .sort()
115
+ .join("\n");
116
+}
117
+
118
+/** A queued message waits for the worker's upload instead of starting a second one. */
119
+export function queuedAttachmentsStillUploading(input: {
120
+ readonly environmentId: EnvironmentId;
121
+ readonly attachments: ReadonlyArray<DraftComposerAttachment>;
122
+ readonly states: Readonly<Record<string, ComposerAttachmentUploadState>>;
123
+}): boolean {
124
+ return input.attachments.some(
125
+ (attachment) =>
126
+ attachment.uploadedAttachmentId === undefined &&
127
+ input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)]?.status ===
128
+ "uploading",
129
+ );
130
+}
131
+
132
export function createComposerAttachmentUploadQueue(options: {
133
readonly upload: (
134
request: ComposerAttachmentUploadRequest,
apps/mobile/src/state/composer-attachment-upload-states.ts
new
+8
@@ -0,0 +1,8 @@
1
+import { Atom } from "effect/reactivity";
2
+
3
+import type { ComposerAttachmentUploadState } from "../lib/composerAttachmentUploadQueue";
4
+
5
+/** The upload worker's per-attachment states, apart from the worker so the outbox can read them. */
6
+export const composerAttachmentUploadsAtom = Atom.make<
7
+ Readonly<Record<string, ComposerAttachmentUploadState>>
8
+>({}).pipe(Atom.keepAlive);
apps/mobile/src/state/composer-attachment-uploads.test.ts
+29
@@ -190,6 +190,7 @@ import {
190
} from "./composer-attachment-uploads";
191
import {
192
enqueueThreadOutboxMessage,
193
+ flattenQueuedThreadMessages,
194
flushThreadOutbox,
195
threadOutboxManager,
196
} from "./thread-outbox";
@@ -216,6 +217,7 @@ const requestsAtom = Atom.make((get) =>
217
composerAttachmentUploadRequests({
218
drafts: get(drafts.composerDraftUploadSourcesAtom),
219
queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
220
+ editingQueuedMessageIds: {},
221
knownEnvironmentIds: [environmentId],
222
connectedEnvironmentIds: new Set([environmentId]),
223
operableEnvironmentIds: new Set([environmentId]),
@@ -319,6 +321,31 @@ it.each(["decode", "write"] as const)(
321
},
322
);
323
324
+it("keeps an upload running when its message is sent mid-upload and records it on the message", async () => {
325
+ const transferStarted = Promise.withResolvers<void>();
326
+ const finishTransfer = Promise.withResolvers<{ status: number }>();
327
+ native.transfer.mockImplementationOnce(async (uri, url) => {
328
+ native.server.set(url.split("/").at(-1)!, native.files.get(uri)!);
329
+ transferStarted.resolve();
330
+ return finishTransfer.promise;
331
+ });
332
+ const queue = worker();
333
+ await transferStarted.promise;
334
+
335
+ // Send enqueues the message, then clears the draft that owned the upload.
336
+ await enqueueThreadOutboxMessage(queuedMessage());
337
+ drafts.clearComposerDraftContent(draftKey, { deferAttachmentCleanup: true });
338
+ finishTransfer.resolve({ status: 204 });
339
+ await queue.settled();
340
+
341
+ expect(native.transfer).toHaveBeenCalledOnce();
342
+ expect(native.deleted).toEqual([]);
343
+ const [queued] = flattenQueuedThreadMessages(
344
+ appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
345
+ );
346
+ expect(queued?.attachments[0]?.uploadedAttachmentId).toBe("pending-1");
347
+});
348
+
349
it.each(["http", "permission"] as const)(
350
"keeps a real %s upload failure blocking Send",
351
async (failure) => {
@@ -355,6 +382,7 @@ it("uploads draft attachments for threads whose ids contain colons", () => {
382
const requests = composerAttachmentUploadRequests({
383
drafts: Object.fromEntries(keys.map((key) => [key, { attachments: [{ ...image, id: key }] }])),
384
queuedMessagesByThreadKey: {},
385
+ editingQueuedMessageIds: {},
386
knownEnvironmentIds: [environmentId],
387
connectedEnvironmentIds: new Set([environmentId]),
388
operableEnvironmentIds: new Set([environmentId]),
@@ -371,6 +399,7 @@ it("does not upload a disconnected colon environment's draft to its connected pr
399
const requests = composerAttachmentUploadRequests({
400
drafts: { [`${disconnected}:thread-1`]: { attachments: [image] } },
401
queuedMessagesByThreadKey: {},
402
+ editingQueuedMessageIds: {},
403
knownEnvironmentIds: [connected, disconnected],
404
connectedEnvironmentIds: new Set([connected]),
405
operableEnvironmentIds: new Set([connected]),
apps/mobile/src/state/composer-attachment-uploads.ts
+90
-19
@@ -15,11 +15,18 @@ import {
15
composerDraftEnvironmentId,
16
canUploadComposerAttachment,
17
createComposerAttachmentUploadQueue,
18
- type ComposerAttachmentUploadState,
18
} from "../lib/composerAttachmentUploadQueue";
19
import { appAtomRegistry } from "./atom-registry";
20
+import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states";
21
import { environmentServerConfigsAtom } from "./server";
22
-import { flattenQueuedThreadMessages, threadOutboxManager } from "./thread-outbox";
22
+import {
23
+ flattenQueuedThreadMessages,
24
+ threadOutboxManager,
25
+ threadOutboxRevision,
26
+ updateThreadOutboxMessage,
27
+ type QueuedThreadMessage,
28
+} from "./thread-outbox";
29
+import { editingQueuedMessageIdsAtom } from "./use-thread-outbox";
30
import {
31
ComposerDraftPersistenceError,
32
type ComposerDraft,
@@ -39,9 +46,7 @@ export {
46
composerAttachmentsStillUploading,
47
} from "../lib/composerAttachmentUploadQueue";
48
42
-export const composerAttachmentUploadsAtom = Atom.make<
43
- Readonly<Record<string, ComposerAttachmentUploadState>>
44
->({}).pipe(Atom.keepAlive);
49
+export { composerAttachmentUploadsAtom };
50
const uploadStateAtom = Atom.family((key: string) =>
51
Atom.map(composerAttachmentUploadsAtom, (states) => states[key]),
52
);
@@ -69,6 +74,8 @@ type ComposerAttachmentUploadRequest = {
74
export function composerAttachmentUploadRequests(input: {
75
readonly drafts: Readonly<Record<string, Pick<ComposerDraft, "attachments" | "project">>>;
76
readonly queuedMessagesByThreadKey: Parameters<typeof flattenQueuedThreadMessages>[0];
77
+ /** An edited message's attachments upload through its editor draft. */
78
+ readonly editingQueuedMessageIds: Readonly<Record<string, true>>;
79
/** Every environment the client knows, so a draft key never resolves to a shorter connected prefix. */
80
readonly knownEnvironmentIds: Iterable<EnvironmentId>;
81
readonly connectedEnvironmentIds: ReadonlySet<EnvironmentId>;
@@ -79,20 +86,82 @@ export function composerAttachmentUploadRequests(input: {
86
>;
87
}): ReadonlyArray<ComposerAttachmentUploadRequest> {
88
const queued = flattenQueuedThreadMessages(input.queuedMessagesByThreadKey);
82
- return Object.entries(input.drafts).flatMap(([key, draft]) => {
83
- const environmentId = composerDraftEnvironmentId(key, input.knownEnvironmentIds, queued, draft);
84
- if (
85
- environmentId === null ||
86
- !input.connectedEnvironmentIds.has(environmentId) ||
87
- !input.operableEnvironmentIds.has(environmentId)
88
- )
89
- return [];
90
- return draft.attachments
91
- .filter((attachment) =>
92
- canUploadComposerAttachment(attachment, input.serverConfigs.get(environmentId)),
93
- )
94
- .map((attachment) => ({ environmentId, attachment }));
95
- });
89
+ const uploadable = (
90
+ environmentId: EnvironmentId | null,
91
+ attachments: ReadonlyArray<DraftComposerAttachment>,
92
+ ): ReadonlyArray<ComposerAttachmentUploadRequest> =>
93
+ environmentId === null ||
94
+ !input.connectedEnvironmentIds.has(environmentId) ||
95
+ !input.operableEnvironmentIds.has(environmentId)
96
+ ? []
97
+ : attachments
98
+ .filter((attachment) =>
99
+ canUploadComposerAttachment(attachment, input.serverConfigs.get(environmentId)),
100
+ )
101
+ .map((attachment) => ({ environmentId, attachment }));
102
+ return [
103
+ ...Object.entries(input.drafts).flatMap(([key, draft]) =>
104
+ uploadable(
105
+ composerDraftEnvironmentId(key, input.knownEnvironmentIds, queued, draft),
106
+ draft.attachments,
107
+ ),
108
+ ),
109
+ // Sending mid-upload hands the attachment to the outbox; requesting it here keeps that
110
+ // upload running instead of aborting it for the outbox to start over.
111
+ ...queued.flatMap((message) =>
112
+ input.editingQueuedMessageIds[message.messageId]
113
+ ? []
114
+ : uploadable(
115
+ message.environmentId,
116
+ message.attachments.filter(
117
+ (attachment) => attachment.uploadedAttachmentId === undefined,
118
+ ),
119
+ ),
120
+ ),
121
+ ];
122
+}
123
+
124
+function withUploadedAttachment(
125
+ message: QueuedThreadMessage,
126
+ uploaded: DraftComposerAttachment,
127
+): QueuedThreadMessage | null {
128
+ const awaiting = (attachment: DraftComposerAttachment) =>
129
+ attachment.id === uploaded.id && attachment.uploadedAttachmentId === undefined;
130
+ if (!message.attachments.some(awaiting)) return null;
131
+ return {
132
+ ...message,
133
+ attachments: message.attachments.map((attachment) =>
134
+ awaiting(attachment) ? uploaded : attachment,
135
+ ),
136
+ };
137
+}
138
+
139
+async function recordQueuedAttachmentUpload(
140
+ environmentId: EnvironmentId,
141
+ uploaded: DraftComposerAttachment,
142
+): Promise<boolean> {
143
+ const awaiting = () => {
144
+ const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom);
145
+ return flattenQueuedThreadMessages(
146
+ appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
147
+ ).flatMap((message) => {
148
+ if (message.environmentId !== environmentId || editing[message.messageId]) return [];
149
+ const updated = withUploadedAttachment(message, uploaded);
150
+ return updated === null
151
+ ? []
152
+ : [{ updated, revision: threadOutboxRevision(message.messageId) }];
153
+ });
154
+ };
155
+ let recorded = false;
156
+ // A sibling attachment finishing at the same time bumps the revision; reread and retry.
157
+ for (let attempt = 0; attempt < 3; attempt++) {
158
+ const pending = awaiting();
159
+ if (pending.length === 0) break;
160
+ for (const { updated, revision } of pending) {
161
+ recorded = (await updateThreadOutboxMessage(updated, revision)) || recorded;
162
+ }
163
+ }
164
+ return recorded;
165
}
166
167
// Each connected environment's session is read here so a granted or revoked
@@ -116,6 +185,7 @@ const composerAttachmentUploadRequestsAtom = Atom.make((get) => {
185
return composerAttachmentUploadRequests({
186
drafts: get(composerDraftUploadSourcesAtom),
187
queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
188
+ editingQueuedMessageIds: get(editingQueuedMessageIdsAtom),
189
knownEnvironmentIds: environments.map((environment) => environment.environmentId),
190
connectedEnvironmentIds: new Set(connected.map((environment) => environment.environmentId)),
191
operableEnvironmentIds,
@@ -164,6 +234,7 @@ export function createComposerAttachmentUploadWorker(
234
retained = setComposerDraftAttachmentUpload(key, uploaded) || retained;
235
}
236
}
237
+ retained = (await recordQueuedAttachmentUpload(environmentId, uploaded)) || retained;
238
if (!retained) return "abandon";
239
// Draft-save failure only costs re-upload after restart; the outbox saves references independently, so preserve accepted uploads.
240
await flushComposerDrafts().catch((error: unknown) => {
apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts
+68
@@ -163,6 +163,8 @@ import { appAtomRegistry } from "./atom-registry";
163
import { environmentSession } from "./session";
164
import { dispatchingQueuedMessageIdAtom, editingQueuedMessageIdsAtom } from "./use-thread-outbox";
165
import { createThreadOutboxDelivery } from "./threadOutboxDelivery";
166
+import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states";
167
+import { workspaceConnections } from "./workspace";
168
169
const message = (overrides: Partial<QueuedThreadMessage> = {}): QueuedThreadMessage => ({
170
environmentId: EnvironmentId.make("secondary"),
@@ -651,3 +653,69 @@ describe("compaction before a queued send", () => {
653
expect(remaining()).toEqual([queued]);
654
});
655
});
656
+
657
+describe("queued delivery timing", () => {
658
+ /** Runs `change`, then resolves once the delivery it starts has settled. */
659
+ async function deliveryAfter(change: () => void): Promise<void> {
660
+ const settled = Promise.withResolvers<void>();
661
+ const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdAtom, (id) => {
662
+ if (id === null) settled.resolve();
663
+ });
664
+ change();
665
+ expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).not.toBeNull();
666
+ await settled.promise.finally(release);
667
+ }
668
+
669
+ it("waits for the composer's upload of a sent attachment instead of uploading it again", async () => {
670
+ setOperateGrant("secondary", true);
671
+ const photo = {
672
+ id: "photo-1",
673
+ type: "image" as const,
674
+ name: "photo.png",
675
+ mimeType: "image/png",
676
+ sizeBytes: 3,
677
+ previewUri: "file:///photo.png",
678
+ fileUri: "file:///photo.png",
679
+ };
680
+ appAtomRegistry.set(composerAttachmentUploadsAtom, {
681
+ "secondary:photo-1": { status: "uploading", progress: 0.5 },
682
+ });
683
+ state.cleanups.push(() => appAtomRegistry.set(composerAttachmentUploadsAtom, {}));
684
+ await state.manager.enqueue(message({ attachments: [photo] }));
685
+ startDelivery();
686
+ expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).toBeNull();
687
+
688
+ await deliveryAfter(() =>
689
+ appAtomRegistry.set(composerAttachmentUploadsAtom, {
690
+ "secondary:photo-1": { status: "ready" },
691
+ }),
692
+ );
693
+ expect(state.prepare).toHaveBeenCalledOnce();
694
+ expect(state.start).toHaveBeenCalledOnce();
695
+ expect(remaining()).toEqual([]);
696
+ });
697
+
698
+ it("sends at once when its environment reconnects instead of waiting out the backoff", async () => {
699
+ setOperateGrant("secondary", true);
700
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
701
+ state.cleanups.push(() => vi.useRealTimers());
702
+ const environments = workspaceConnections.environmentsAtom as unknown as Atom.Writable<
703
+ ReadonlyArray<{ environmentId: string; connectionState: string }>
704
+ >;
705
+ const connected = [{ environmentId: "secondary", connectionState: "connected" }];
706
+ state.cleanups.push(() => appAtomRegistry.set(environments, connected));
707
+ await state.manager.enqueue(message());
708
+ state.start.mockResolvedValueOnce(
709
+ AsyncResult.failure(Cause.fail(new Error("Socket is not connected"))),
710
+ );
711
+ await runDrain();
712
+ expect(state.start).toHaveBeenCalledOnce();
713
+
714
+ appAtomRegistry.set(environments, [
715
+ { environmentId: "secondary", connectionState: "disconnected" },
716
+ ]);
717
+ await deliveryAfter(() => appAtomRegistry.set(environments, connected));
718
+ expect(state.start).toHaveBeenCalledTimes(2);
719
+ expect(remaining()).toEqual([]);
720
+ });
721
+});
apps/mobile/src/state/threadOutboxDelivery.ts
+39
@@ -48,6 +48,11 @@ import {
48
} from "./pending-thread-creation";
49
import { environmentProjects } from "./projects";
50
import { environmentServerConfigsAtom, serverEnvironment } from "./server";
51
+import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states";
52
+import {
53
+ queuedAttachmentsStillUploading,
54
+ uploadingComposerAttachmentKeys,
55
+} from "../lib/composerAttachmentUploadQueue";
56
import { gitEnvironment } from "./git";
57
import { environmentSession, readEnvironmentScope, sessionHasScope } from "./session";
58
import {
@@ -589,6 +594,11 @@ async function preserveUploadedAttachmentsForEditor(
594
595
// Everything a drain pass decides from. Each queued environment's session is
596
// read here so a granted or revoked operate scope re-runs the drain.
597
+const uploadingComposerAttachmentKeysAtom = Atom.map(
598
+ composerAttachmentUploadsAtom,
599
+ uploadingComposerAttachmentKeys,
600
+);
601
+
602
const deliveryInputsAtom = Atom.make((get) => {
603
const connectedEnvironments = get(workspaceConnections.environmentsAtom);
604
const operableEnvironments = new Set<EnvironmentId>();
@@ -612,6 +622,8 @@ const deliveryInputsAtom = Atom.make((get) => {
622
serverConfigs: get(environmentServerConfigsAtom),
623
connectedEnvironments,
624
operableEnvironments,
625
+ // Wakes a message waiting on the upload worker when that upload settles.
626
+ uploadingAttachmentKeys: get(uploadingComposerAttachmentKeysAtom),
627
};
628
}).pipe(Atom.withLabel("mobile:thread-outbox:delivery-inputs"));
629
@@ -691,6 +703,7 @@ export function createThreadOutboxDelivery(): {
703
} {
704
let disposed = false;
705
const retries = new Map<MessageId, ThreadOutboxRetry>();
706
+ let readyEnvironmentIds = new Set<EnvironmentId>();
707
// Existing-thread turns the server accepted but whose outbox removal has
708
// not landed; the next pass retries only the removal, never the send.
709
const acknowledgedExistingThreadMessageIds = new Set<MessageId>();
@@ -1192,6 +1205,23 @@ export function createThreadOutboxDelivery(): {
1205
connectedEnvironments,
1206
operableEnvironments,
1207
} = appAtomRegistry.get(deliveryInputsAtom);
1208
+ // Failures while an environment was unreachable say nothing about it now; a backoff
1209
+ // started then would hold a message for up to its full delay after reconnecting.
1210
+ const ready = new Set(
1211
+ connectedEnvironments
1212
+ .filter(
1213
+ (environment) =>
1214
+ environment.connectionState === "connected" &&
1215
+ serverConfigs.has(environment.environmentId),
1216
+ )
1217
+ .map((environment) => environment.environmentId),
1218
+ );
1219
+ for (const message of Object.values(queuedMessagesByThreadKey).flat()) {
1220
+ if (ready.has(message.environmentId) && !readyEnvironmentIds.has(message.environmentId)) {
1221
+ clearRetry(message.messageId);
1222
+ }
1223
+ }
1224
+ readyEnvironmentIds = ready;
1225
if (dispatchingQueuedMessageId !== null) {
1226
return;
1227
}
@@ -1255,6 +1285,15 @@ export function createThreadOutboxDelivery(): {
1285
if (retryNotBefore(messageId) > Date.now()) {
1286
continue;
1287
}
1288
+ if (
1289
+ queuedAttachmentsStillUploading({
1290
+ environmentId: nextQueuedMessage.environmentId,
1291
+ attachments: nextQueuedMessage.attachments,
1292
+ states: appAtomRegistry.get(composerAttachmentUploadsAtom),
1293
+ })
1294
+ ) {
1295
+ continue;
1296
+ }
1297
1298
let thread = findThread(threads, nextQueuedMessage);
1299
if (thread && scopedThreadKey(thread.environmentId, thread.id) !== threadKey) {
apps/mobile/src/state/use-thread-composer-state.ts
+5
-4
@@ -63,7 +63,7 @@ import {
63
pruneCaughtUpAcknowledgedThreadMessages,
64
} from "./acknowledged-thread-messages";
65
import {
66
- appendPendingThreadMessages,
66
+ appendAcceptedThreadMessages,
67
selectPendingAcknowledgedMessages,
68
} from "../features/threads/pending-thread-feed";
69
import { threadAllowsProviderSwitch } from "./thread-provider-switching";
@@ -294,9 +294,10 @@ export function useThreadComposerState(input: {
294
sequence: selectedThreadSequence,
295
projectedMessages: selectedThreadMessages,
296
});
297
- if (pendingAcknowledgments.length === 0) return feed;
298
- return appendPendingThreadMessages(feed, feed, pendingAcknowledgments).map((entry) =>
299
- entry.pendingMessage ? { ...entry, acknowledged: true } : entry,
297
+ return appendAcceptedThreadMessages(
298
+ feed,
299
+ pendingAcknowledgments,
300
+ pendingCreation.length > 0 ? pendingCreationMessage : null,
301
);
302
})();
303