@devries-cameron20 / t3code-1 / commits / b90ffeaf28
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): drop duplicate attachments, as on web (#29143)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

ahalekelly committed Nov 14, 2026 at 04:41 UTC b90ffeaf2887b41fdedecfd170d7701e3822d9b9
9 files changed +71 -22
apps/mobile/src/components/ComposerEditor.tsx
+1 -1
@@ -76,7 +76,7 @@ export function ComposerEditor({
76 props.onChangeText(text);
77 if (draftKey) {
78 setComposerDraftContext(draftKey, restored.context);
79 - appendComposerDraftAttachments(draftKey, restored.attachments, { allowOverflow: true });
79 + appendComposerDraftAttachments(draftKey, restored.attachments, { restore: true });
80 }
81 };
82 const [selected, setSelected] = useState<{ source: string; start: number; end: number } | null>(
apps/mobile/src/features/threads/QuestionAttachments.tsx
+1 -4
@@ -105,10 +105,7 @@ export function QuestionAttachments(props: {
105 props.question.id,
106 );
107 changeQuestionAttachmentPreparation(key, 1);
108 - void convertPastedImagesToAttachments({
109 - uris,
110 - existingCount: appAtomRegistry.get(composerDraftsAtom)[key]?.attachments.length ?? 0,
111 - })
108 + void convertPastedImagesToAttachments({ uris })
109 .then(async (result) => {
110 if (
111 scope.active &&
apps/mobile/src/lib/composerImages.ts
+6 -2
@@ -569,15 +569,19 @@ function isOwnedPastedImageUri(uri: string): boolean {
569 }
570 }
571
572 +/** Leave out existingCount when the draft store caps after its duplicate check. */
573 export async function convertPastedImagesToAttachments(input: {
574 readonly uris: ReadonlyArray<string>;
574 - readonly existingCount: number;
575 + readonly existingCount?: number;
576 }): Promise<{
577 readonly attachments: ReadonlyArray<DraftComposerImageAttachment>;
578 readonly error: string | null;
579 }> {
580 const { File } = await import("expo-file-system");
580 - const remainingSlots = PROVIDER_SEND_TURN_MAX_ATTACHMENTS - input.existingCount;
581 + const remainingSlots =
582 + input.existingCount === undefined
583 + ? Number.POSITIVE_INFINITY
584 + : PROVIDER_SEND_TURN_MAX_ATTACHMENTS - input.existingCount;
585 const attachments: DraftComposerImageAttachment[] = [];
586 let error: string | null = null;
587
apps/mobile/src/state/recover-failed-thread-draft.ts
+1 -1
@@ -26,7 +26,7 @@ export async function recoverFailedThreadDraft(message: QueuedThreadMessage): Pr
26 appendComposerDraftAttachments(
27 targetKey,
28 source.attachments.filter((attachment) => !existingIds.has(attachment.id)),
29 - { allowOverflow: true },
29 + { restore: true },
30 );
31 // Recovery may exceed caps; preserve every file and leave trimming to the editor.
32 await flushComposerDrafts();
apps/mobile/src/state/threadOutboxDelivery.ts
+1 -1
@@ -372,7 +372,7 @@ export async function recoverEditedCreationAfterDelivery(
372 appendComposerDraftAttachments(
373 draftKey,
374 kept.attachments.filter((attachment) => !existingAttachmentIds.has(attachment.id)),
375 - { allowOverflow: true },
375 + { restore: true },
376 );
377 updateComposerDraftSettings(draftKey, {
378 ...(kept.modelSelection !== undefined ? { modelSelection: kept.modelSelection } : {}),
apps/mobile/src/state/use-composer-attachment-actions.ts
+1 -4
@@ -101,10 +101,7 @@ export function useComposerAttachmentActions(props: {
101 const insertion = captureComposerDraftInsertion(key);
102 await withDraftOperation(key, async (signal) => {
103 try {
104 - const result = await convertPastedImagesToAttachments({
105 - uris,
106 - existingCount: countComposerDraftAttachmentsAfterSelection(key, insertion),
107 - });
104 + const result = await convertPastedImagesToAttachments({ uris });
105 report(
106 "Could not paste images",
107 result.error,
apps/mobile/src/state/use-composer-drafts.test.ts
+35 -2
@@ -458,7 +458,7 @@ describe("mobile composer drafts", () => {
458 const restored = history.restore(text, getComposerDraftSnapshot(key));
459 setComposerDraftText(key, text);
460 setComposerDraftContext(key, restored.context);
461 - appendComposerDraftAttachments(key, restored.attachments, { allowOverflow: true });
461 + appendComposerDraftAttachments(key, restored.attachments, { restore: true });
462 };
463 try {
464 changeText("");
@@ -1054,12 +1054,45 @@ describe("mobile composer drafts", () => {
1054 const overflowRejected = appendComposerDraftAttachments(
1055 draftKey,
1056 [makeAttachment("restored-1")],
1057 - { allowOverflow: true },
1057 + { restore: true },
1058 );
1059 expect(overflowRejected).toBe(0);
1060 expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(101);
1061 });
1062
1063 + it("drops duplicate attachments, releases their files, and keeps them on restore", async () => {
1064 + const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true);
1065 + onTestFinished(() => outboxLoad.mockRestore());
1066 + const cleanup = Promise.withResolvers<void>();
1067 + composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => {
1068 + cleanup.resolve();
1069 + });
1070 + const makeVideo = (id: string) => ({
1071 + id,
1072 + type: "file" as const,
1073 + name: "clip.mov",
1074 + mimeType: "video/quicktime",
1075 + sizeBytes: 42,
1076 + fileUri: `file:///documents/t3-composer-attachments/${id}.mov`,
1077 + });
1078 + const draftKey = "new-task:environment-1:project-duplicates";
1079 + appAtomRegistry.set(composerDraftsAtom, {
1080 + [draftKey]: { text: "", attachments: [makeVideo("held")] },
1081 + });
1082 +
1083 + expect(
1084 + appendComposerDraftAttachments(draftKey, [makeVideo("again"), makeVideo("again-2")]),
1085 + ).toBe(0);
1086 + expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toEqual([
1087 + makeVideo("held"),
1088 + ]);
1089 + await cleanup.promise;
1090 + expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledWith(makeVideo("again").fileUri);
1091 +
1092 + appendComposerDraftAttachments(draftKey, [makeVideo("restored")], { restore: true });
1093 + expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(2);
1094 + });
1095 +
1096 it("keeps shared attachment files until every draft releases them", async () => {
1097 const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true);
1098 onTestFinished(() => outboxLoad.mockRestore());
apps/mobile/src/state/use-composer-drafts.ts
+24 -6
@@ -1373,28 +1373,46 @@ export function appendComposerDraftText(draftKey: string, value: string): void {
1373 });
1374 }
1375
1376 -/** Restore with allowOverflow to preserve every attachment after failed sends. */
1376 +function attachmentDedupKey(attachment: DraftComposerAttachment): string {
1377 + return `${attachment.mimeType}\u0000${attachment.sizeBytes}\u0000${attachment.name}`;
1378 +}
1379 +
1380 +/**
1381 + * Restore puts back attachments after failed sends, so it skips the duplicate check and the caps.
1382 + * Returns how many attachments the caps rejected.
1383 + */
1384 export function appendComposerDraftAttachments(
1385 draftKey: string,
1379 - attachments: ReadonlyArray<DraftComposerAttachment>,
1386 + incoming: ReadonlyArray<DraftComposerAttachment>,
1387 options?: {
1381 - readonly allowOverflow?: boolean;
1388 + readonly restore?: boolean;
1389 readonly appendReference?: boolean;
1390 readonly insertion?: ComposerDraftInsertion;
1391 readonly maxAttachments?: number;
1392 },
1393 ): number {
1387 - if (attachments.length === 0) {
1394 + if (incoming.length === 0) {
1395 return 0;
1396 }
1397 let rejected: ReadonlyArray<DraftComposerAttachment> = [];
1398 let removed: ReadonlyArray<DraftComposerAttachment> = [];
1399 + let duplicates: ReadonlyArray<DraftComposerAttachment> = [];
1400 updateComposerDrafts((current) => {
1401 const existing = normalizeDraft(current[draftKey]);
1402 const retained = options?.appendReference
1403 ? draftWithoutInsertionSelection(draftKey, existing, options.insertion)
1404 : existing;
1397 - const remaining = options?.allowOverflow
1405 + const keys = new Set(retained.attachments.map(attachmentDedupKey));
1406 + const attachments = options?.restore
1407 + ? incoming
1408 + : incoming.filter((attachment) => {
1409 + const key = attachmentDedupKey(attachment);
1410 + if (keys.has(key)) return false;
1411 + keys.add(key);
1412 + return true;
1413 + });
1414 + duplicates = incoming.filter((attachment) => !attachments.includes(attachment));
1415 + const remaining = options?.restore
1416 ? attachments.length
1417 : Math.max(
1418 0,
@@ -1437,7 +1455,7 @@ export function appendComposerDraftAttachments(
1455 [draftKey]: draft,
1456 };
1457 });
1440 - scheduleUnusedComposerAttachmentCleanup([...rejected, ...removed]);
1458 + scheduleUnusedComposerAttachmentCleanup([...rejected, ...removed, ...duplicates]);
1459 return rejected.length;
1460 }
1461
apps/mobile/src/state/use-thread-composer-state.ts
+1 -1
@@ -832,7 +832,7 @@ export function useThreadComposerState(input: {
832 context: draft.context,
833 attachments: [],
834 });
835 - appendComposerDraftAttachments(threadKey, attachments, { allowOverflow: true });
835 + appendComposerDraftAttachments(threadKey, attachments, { restore: true });
836 if (freshContext) setFreshSessionPending(threadKey, true);
837 setThreadComposerError(
838 threadKey,