The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): a retried document upload keeps its tile and shows progress (#38553)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 18, 2026 at 12:12 UTC
5552f0aa74b1cdc0606b86374d4312c42e3b117a
5 files changed
+91
-45
apps/mobile/src/features/threads/ThreadComposer.tsx
+1
-3
@@ -323,9 +323,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
323
const uploadStates = useAtomValue(composerAttachmentUploadsAtom);
324
const stripAttachments = composerStripAttachments(
325
draftAttachments,
326
- (attachment) =>
327
- uploadStates[composerAttachmentUploadKey(props.environmentId, attachment.id)]?.status ===
328
- "failed",
326
+ (attachment) => uploadStates[composerAttachmentUploadKey(props.environmentId, attachment.id)],
327
);
328
const attachmentsUploading =
329
props.connectionState === "connected" &&
apps/mobile/src/lib/composerImages.test.ts
+17
-6
@@ -3,6 +3,7 @@ import {
3
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
4
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
5
} from "@t3tools/contracts";
6
+import type { ComposerAttachmentUploadState } from "./composerAttachmentUploadQueue";
7
8
const files = new Map<
9
string,
@@ -333,15 +334,25 @@ describe("composerStripAttachments", () => {
334
it("keeps media beside a document rather than dropping the whole strip", async () => {
335
const { composerStripAttachments } = await import("./composerImages");
336
expect(
336
- composerStripAttachments([doc, image, video] as never, () => false).map((a) => a.id),
337
+ composerStripAttachments([doc, image, video] as never, () => ({
338
+ status: "uploading",
339
+ progress: 0.5,
340
+ })).map((a) => a.id),
341
).toEqual(["img-1", "vid-1"]);
342
});
343
340
- it("shows a document whose upload failed, so it can be retried", async () => {
344
+ it("shows a document whose upload failed until its retry finishes", async () => {
345
const { composerStripAttachments } = await import("./composerImages");
342
- expect(
343
- composerStripAttachments([doc, image] as never, (a) => a.id === "doc-1").map((a) => a.id),
344
- ).toEqual(["doc-1", "img-1"]);
346
+ const strip = (upload: ComposerAttachmentUploadState) =>
347
+ composerStripAttachments([doc, image] as never, (a) =>
348
+ a.id === "doc-1" ? upload : undefined,
349
+ ).map((a) => a.id);
350
+ expect(strip({ status: "failed", reason: "Service unavailable" })).toEqual(["doc-1", "img-1"]);
351
+ expect(strip({ status: "uploading", progress: 0.4, retrying: true })).toEqual([
352
+ "doc-1",
353
+ "img-1",
354
+ ]);
355
+ expect(strip({ status: "ready" })).toEqual(["img-1"]);
356
});
357
358
it("keeps oversized document-picked images visible while sending them as files", async () => {
@@ -356,7 +367,7 @@ describe("composerStripAttachments", () => {
367
sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
368
};
369
359
- expect(composerStripAttachments([oversizedImage], () => false)).toEqual([oversizedImage]);
370
+ expect(composerStripAttachments([oversizedImage], () => undefined)).toEqual([oversizedImage]);
371
expect(isComposerImageAttachment(oversizedImage)).toBe(false);
372
});
373
});
apps/mobile/src/lib/composerImages.ts
+15
-7
@@ -12,6 +12,7 @@ import {
12
type PastedTextAttachmentSource,
13
type UploadChatImageAttachment,
14
} from "@t3tools/contracts";
15
+import type { AttachmentUploadStatus } from "@t3tools/client-runtime/state/attachment-uploads";
16
import type { DocumentPickerResult } from "expo-document-picker";
17
import { msg } from "gt-react-native";
18
import { m } from "../i18n/messages";
@@ -82,19 +83,26 @@ export type DraftComposerAttachment = DraftComposerImageAttachment | DraftCompos
83
84
/**
85
* Non-media attachments require chip references; missing references mean lost draft files.
85
- * A chip cannot retry an upload, so a document whose upload failed joins the strip.
86
+ * A chip cannot retry an upload or show its progress, so a document whose upload failed joins
87
+ * the strip until its retry finishes.
88
*/
89
export function composerStripAttachments(
90
attachments: ReadonlyArray<DraftComposerAttachment>,
89
- uploadFailed: (attachment: DraftComposerAttachment) => boolean,
91
+ uploadStatus: (attachment: DraftComposerAttachment) => AttachmentUploadStatus | undefined,
92
): ReadonlyArray<DraftComposerAttachment> {
91
- return attachments.filter(
92
- (attachment) =>
93
+ return attachments.filter((attachment) => {
94
+ if (
95
attachment.type === "image" ||
96
imageMimeType(attachment) !== null ||
95
- videoMimeType(attachment) !== null ||
96
- uploadFailed(attachment),
97
- );
97
+ videoMimeType(attachment) !== null
98
+ ) {
99
+ return true;
100
+ }
101
+ const upload = uploadStatus(attachment);
102
+ return (
103
+ upload?.status === "failed" || (upload?.status === "uploading" && upload.retrying === true)
104
+ );
105
+ });
106
}
107
108
/** Promote legacy document-picked images only when their originals fit the image contract. */
packages/client-runtime/src/state/attachmentUploads.test.ts
+19
-5
@@ -210,8 +210,16 @@ describe("attachment upload registry", () => {
210
reason: "Server unavailable",
211
});
212
expect(progress).toEqual([0, 0.1, 1]);
213
- upload.mockImplementationOnce(async () => "uploaded");
213
+ upload.mockImplementationOnce(async (_request, _signal, report) => {
214
+ report(0.5);
215
+ return "uploaded";
216
+ });
217
queue.retry(uploadKey(environmentId, "file"));
218
+ expect(states[uploadKey(environmentId, "file")]).toEqual({
219
+ status: "uploading",
220
+ progress: 0.5,
221
+ retrying: true,
222
+ });
223
await queue.settled();
224
expect(states[uploadKey(environmentId, "file")]).toEqual({ status: "ready" });
225
queue.dispose();
@@ -251,18 +259,24 @@ describe("attachment upload registry", () => {
259
);
260
it.effect("restarts a failed upload when a send joins it instead of rejoining the failure", () =>
261
Effect.gen(function* () {
262
+ const replacement = Promise.withResolvers<string>();
263
const upload = vi
264
.fn()
265
.mockRejectedValueOnce(new Error("Server unavailable"))
257
- .mockResolvedValue("uploaded-2");
266
+ .mockImplementationOnce(() => replacement.promise);
267
const queue = createQueue({ upload, onChange: () => {} });
268
const local = request("photo");
260
- queue.sync([local]);
269
+ const key = uploadKey(environmentId, local.attachment.id);
270
+ const release = queue.hold(local);
271
yield* Effect.promise(() => queue.settled());
262
- expect(queue.status(uploadKey(environmentId, "photo"))?.status).toBe("failed");
272
+ expect(queue.status(key)?.status).toBe("failed");
273
264
- expect(yield* queue.upload(local)).toBe("uploaded-2");
274
+ const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true });
275
+ expect(queue.status(key)).toEqual({ status: "uploading", progress: 0, retrying: true });
276
+ replacement.resolve("uploaded-2");
277
+ expect(yield* Fiber.join(sent)).toBe("uploaded-2");
278
expect(upload).toHaveBeenCalledTimes(2);
279
+ release();
280
queue.dispose();
281
}),
282
);
packages/client-runtime/src/state/attachmentUploads.ts
+39
-24
@@ -29,17 +29,23 @@ export function attachmentUploadError(cause: unknown): AttachmentUploadError {
29
}
30
31
export type AttachmentUploadStatus =
32
- | { readonly status: "uploading"; readonly progress: number }
32
+ /** `retrying` marks an upload started again after it failed. */
33
+ | { readonly status: "uploading"; readonly progress: number; readonly retrying?: true }
34
| { readonly status: "ready" }
35
| { readonly status: "failed"; readonly reason: string };
36
36
-/** Equal by key; the request rides along for the upload. */
37
+const uploading = (progress: number, retry: boolean): AttachmentUploadStatus =>
38
+ retry ? { status: "uploading", progress, retrying: true } : { status: "uploading", progress };
39
+
40
+/** Equal by id; the request and retry flag only matter when starting a transfer. */
41
class UploadKey<Request> implements Equal.Equal {
42
readonly id: string;
43
readonly request: Request | null;
40
- constructor(id: string, request: Request | null) {
44
+ readonly retry: boolean;
45
+ constructor(id: string, request: Request | null, retry = false) {
46
this.id = id;
47
this.request = request;
48
+ this.retry = retry;
49
}
50
[Equal.symbol](that: unknown): boolean {
51
return that instanceof UploadKey && that.id === this.id;
@@ -78,7 +84,8 @@ export function createAttachmentUploadRegistry<Request>(options: {
84
previous === status ||
85
(previous?.status === "uploading" &&
86
status?.status === "uploading" &&
81
- previous.progress === status.progress)
87
+ previous.progress === status.progress &&
88
+ previous.retrying === status.retrying)
89
)
90
return;
91
const next = { ...statuses };
@@ -109,13 +116,14 @@ export function createAttachmentUploadRegistry<Request>(options: {
116
const request = key.request!;
117
return Effect.suspend(() => {
118
if (statuses[key.id]?.status !== "uploading") {
112
- setOwnStatus(owner, key.id, { status: "uploading", progress: 0 });
119
+ setOwnStatus(owner, key.id, uploading(0, key.retry));
120
}
121
return options.upload(request, (progress) =>
115
- setOwnStatus(owner, key.id, {
116
- status: "uploading",
117
- progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20,
118
- }),
122
+ setOwnStatus(
123
+ owner,
124
+ key.id,
125
+ uploading(Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, key.retry),
126
+ ),
127
);
128
}).pipe(
129
Semaphore.withPermits(permitsFor(options.environmentId(request)), 1),
@@ -134,7 +142,7 @@ export function createAttachmentUploadRegistry<Request>(options: {
142
lookup: Effect.fn("AttachmentUploads.lookup")(function* (key: UploadKey<Request>) {
143
const owner = {};
144
owners.set(key.id, owner);
137
- setStatus(key.id, { status: "uploading", progress: 0 });
145
+ setStatus(key.id, uploading(0, key.retry));
146
yield* Effect.addFinalizer(() =>
147
Effect.sync(() => {
148
setOwnStatus(owner, key.id, undefined);
@@ -154,14 +162,11 @@ export function createAttachmentUploadRegistry<Request>(options: {
162
}),
163
),
164
);
157
- const keyOf = (request: Request) => new UploadKey(options.key(request), request);
158
-
165
let disposed = false;
160
- /** Keeps the request's upload running until the returned release is called. */
161
- function hold(request: Request): () => void {
166
+ function holdUpload(key: UploadKey<Request>): () => void {
167
if (disposed) return () => {};
168
const holder = Scope.makeUnsafe();
164
- Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, keyOf(request))));
169
+ Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, key)));
170
let released = false;
171
return () => {
172
if (released) return;
@@ -172,8 +177,7 @@ export function createAttachmentUploadRegistry<Request>(options: {
177
178
const synced = new Map<string, () => void>();
179
let requested: ReadonlyArray<Request> = [];
175
- /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */
176
- function sync(requests: ReadonlyArray<Request>) {
180
+ function syncUploads(requests: ReadonlyArray<Request>, retryKey?: string) {
181
if (disposed) return;
182
requested = requests;
183
const keys = new Map(requests.map((request) => [options.key(request), request]));
@@ -183,7 +187,7 @@ export function createAttachmentUploadRegistry<Request>(options: {
187
release();
188
}
189
for (const [id, request] of keys) {
186
- if (!synced.has(id)) synced.set(id, hold(request));
190
+ if (!synced.has(id)) synced.set(id, holdUpload(new UploadKey(id, request, id === retryKey)));
191
}
192
}
193
@@ -192,17 +196,24 @@ export function createAttachmentUploadRegistry<Request>(options: {
196
Effect.runSync(RcMap.invalidate(uploads, new UploadKey<Request>(key, null)));
197
}
198
195
- /** Drops the key's upload and starts it again for whatever `sync` holds. */
199
+ /** Drops the key's upload and replaces it if `sync` holds it. */
200
function restart(key: string) {
201
+ const retry = statuses[key]?.status === "failed";
202
invalidate(key);
203
synced.get(key)?.();
204
synced.delete(key);
200
- sync(requested);
205
+ syncUploads(requested, retry ? key : undefined);
206
}
207
208
return {
204
- hold,
205
- sync,
209
+ /** Keeps the request's upload running until the returned release is called. */
210
+ hold(request: Request): () => void {
211
+ return holdUpload(new UploadKey(options.key(request), request));
212
+ },
213
+ /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */
214
+ sync(requests: ReadonlyArray<Request>) {
215
+ syncUploads(requests);
216
+ },
217
invalidate,
218
restart,
219
/**
@@ -213,8 +224,12 @@ export function createAttachmentUploadRegistry<Request>(options: {
224
return Effect.suspend(() => {
225
if (disposed) return Effect.fail(attachmentUploadError(new Error("Upload cancelled.")));
226
const key = options.key(request);
216
- if (statuses[key]?.status === "failed") restart(key);
217
- return RcMap.get(uploads, keyOf(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped);
227
+ const retry = statuses[key]?.status === "failed";
228
+ if (retry) restart(key);
229
+ return RcMap.get(uploads, new UploadKey(key, request, retry)).pipe(
230
+ Effect.flatMap(Fiber.join),
231
+ Effect.scoped,
232
+ );
233
});
234
},
235
/** Starts a failed upload that `sync` holds again. */