Skip to content

Commit 572bdfb

Browse files
committed
fix(mobile): retain image files until inline reads finish
Move legacy image reads into attachmentUpload. Hold the existing file lease through each read and release it in finally. A removed draft can then clean up its file after the read, without breaking the send. Return abandoned if a canceled native read rejects. Add read-barrier coverage through the real ownership cleanup in the existing tests. Created with GPT-6 Astra (preview) in Codex.
1 parent 1267ae5 commit 572bdfb

5 files changed

Lines changed: 190 additions & 163 deletions

File tree

‎apps/mobile/src/lib/attachmentUpload.test.ts‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ vi.mock("../state/atom-registry", () => ({
3131
appAtomRegistry: { get: mocks.readAtom },
3232
}));
3333

34+
// The real read lease and cleanup are covered by the composer ownership suite.
35+
vi.mock("../state/use-composer-drafts", () => ({
36+
retainComposerAttachmentFileForPreview: () => () => {},
37+
}));
38+
3439
vi.mock("../state/assets", () => ({
3540
assetEnvironment: { createUrl: mocks.createAssetUrl },
3641
}));
@@ -246,26 +251,52 @@ describe("prepareTurnAttachments", () => {
246251
expect(mocks.upload).not.toHaveBeenCalled();
247252
});
248253

249-
it("abandons a legacy send canceled while reading image bytes", async () => {
250-
const readStarted = Promise.withResolvers<void>();
251-
const read = Promise.withResolvers<string>();
252-
const controller = new AbortController();
253-
mocks.readBase64.mockImplementation(() => {
254-
readStarted.resolve();
255-
return read.promise;
256-
});
257-
const preparing = prepareTurnAttachments({
254+
it("reads restored images from the current iOS document container for legacy sends", async () => {
255+
const fileName = "33333333-3333-4333-8333-333333333333-photo.png";
256+
mocks.documentUri =
257+
"file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents";
258+
const oldUri = `file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`;
259+
const prepared = await prepareTurnAttachments({
258260
environmentId,
259-
attachments: [fileBackedImage],
260-
signal: controller.signal,
261+
attachments: [{ ...fileBackedImage, fileUri: oldUri, previewUri: oldUri }],
261262
});
262-
await readStarted.promise;
263-
controller.abort();
264-
read.resolve("YWJj");
263+
expect(prepared.status).toBe("ready");
264+
expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith(
265+
`${mocks.documentUri}/t3-composer-attachments/${fileName}`,
266+
);
267+
});
265268

266-
await expect(preparing).resolves.toEqual({ status: "abandoned" });
269+
it("rejects a legacy image without inline bytes or a file", async () => {
270+
const { fileUri: _, ...missingImage } = fileBackedImage;
271+
await expect(
272+
prepareTurnAttachments({ environmentId, attachments: [missingImage] }),
273+
).rejects.toThrow("'photo.png' is no longer available. Attach the image again.");
267274
});
268275

276+
it.each(["resolve", "reject"] as const)(
277+
"abandons a canceled legacy image read that later %ss",
278+
async (outcome) => {
279+
const readStarted = Promise.withResolvers<void>();
280+
const read = Promise.withResolvers<string>();
281+
const controller = new AbortController();
282+
mocks.readBase64.mockImplementation(() => {
283+
readStarted.resolve();
284+
return read.promise;
285+
});
286+
const preparing = prepareTurnAttachments({
287+
environmentId,
288+
attachments: [fileBackedImage],
289+
signal: controller.signal,
290+
});
291+
await readStarted.promise;
292+
controller.abort();
293+
if (outcome === "resolve") read.resolve("YWJj");
294+
else read.reject(new Error("Native read failed after cancellation"));
295+
296+
await expect(preparing).resolves.toEqual({ status: "abandoned" });
297+
},
298+
);
299+
269300
it("uploads a file-backed image from its owned copy without staging base64", async () => {
270301
const prepared = await prepareTurnAttachments({
271302
environmentId,

‎apps/mobile/src/lib/attachmentUpload.ts‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import { appAtomRegistry } from "../state/atom-registry";
2020
import { assetEnvironment } from "../state/assets";
2121
import { attachmentEnvironment } from "../state/attachments";
2222
import { environmentSession } from "../state/session";
23+
import { retainComposerAttachmentFileForPreview } from "../state/use-composer-drafts";
2324
import { resolveOwnedComposerAttachmentFileUri } from "./composerAttachmentFiles";
24-
import { toUploadChatImageAttachments, type DraftComposerAttachment } from "./composerImages";
25+
import {
26+
isFileBackedComposerAttachment,
27+
type DraftComposerAttachment,
28+
type DraftComposerImageAttachment,
29+
} from "./composerImages";
2530
import { uuidv4 } from "./uuid";
2631

2732
/**
@@ -179,6 +184,48 @@ function attachmentUploadInput(attachment: DraftComposerAttachment) {
179184
return { ...fields, mimeType };
180185
}
181186

187+
/**
188+
* Wire shape for startTurn on servers without attachment uploads: pure inline
189+
* uploads without client draft id / previewUri. File-backed images read their
190+
* base64 from disk lazily, only when this legacy path is actually taken.
191+
*/
192+
async function toUploadChatImageAttachments(
193+
attachments: ReadonlyArray<DraftComposerImageAttachment>,
194+
): Promise<ReadonlyArray<UploadChatImageAttachment>> {
195+
return Promise.all(
196+
attachments.map(async (attachment) => ({
197+
type: attachment.type,
198+
name: attachment.name,
199+
mimeType: attachment.mimeType,
200+
sizeBytes: attachment.sizeBytes,
201+
dataUrl: await composerImageAttachmentDataUrl(attachment),
202+
})),
203+
);
204+
}
205+
206+
/** Inline bytes for one image: legacy drafts carry them, file-backed ones read them from disk. */
207+
async function composerImageAttachmentDataUrl(
208+
attachment: DraftComposerImageAttachment,
209+
): Promise<string> {
210+
if (attachment.dataUrl !== undefined) {
211+
return attachment.dataUrl;
212+
}
213+
if (!isFileBackedComposerAttachment(attachment)) {
214+
throw new Error(`'${attachment.name}' is no longer available. Attach the image again.`);
215+
}
216+
const release = retainComposerAttachmentFileForPreview(attachment);
217+
try {
218+
const { File, Paths } = await import("expo-file-system");
219+
const uri =
220+
resolveOwnedComposerAttachmentFileUri(attachment.fileUri, Paths.document.uri) ??
221+
attachment.fileUri;
222+
const base64 = await new File(uri).base64();
223+
return `data:${attachment.mimeType};base64,${base64}`;
224+
} finally {
225+
release();
226+
}
227+
}
228+
182229
async function uploadFileBytes(
183230
attachment: DraftComposerAttachment,
184231
url: string,
@@ -263,11 +310,16 @@ export async function prepareTurnAttachments(input: {
263310
});
264311

265312
if (input.attachments.length === 0 || (files.length === 0 && !input.supportsImageUploads)) {
266-
const imageAttachments = await toUploadChatImageAttachments(
267-
input.attachments.filter((attachment) => attachment.type === "image"),
268-
);
269-
if (input.signal?.aborted) return { status: "abandoned" };
270-
return ready(imageAttachments, [], input.attachments);
313+
try {
314+
const imageAttachments = await toUploadChatImageAttachments(
315+
input.attachments.filter((attachment) => attachment.type === "image"),
316+
);
317+
if (input.signal?.aborted) return { status: "abandoned" };
318+
return ready(imageAttachments, [], input.attachments);
319+
} catch (error) {
320+
if (input.signal?.aborted) return { status: "abandoned" };
321+
throw error;
322+
}
271323
}
272324

273325
const connection = appAtomRegistry.get(

‎apps/mobile/src/lib/composerImages.test.ts‎

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
22
import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS } from "@t3tools/contracts";
33

44
const files = new Map<string, { base64: string; deleted: boolean }>();
5-
const paths = { documentUri: "file:///documents" };
65

76
vi.mock("expo-file-system", () => ({
87
File: class {
@@ -31,114 +30,13 @@ vi.mock("expo-file-system", () => ({
3130
}
3231
}
3332
},
34-
Paths: {
35-
get document() {
36-
return { uri: paths.documentUri };
37-
},
38-
},
3933
}));
4034

4135
vi.mock("./uuid", () => ({
4236
uuidv4: () => "attachment-id",
4337
}));
4438

45-
import {
46-
composerImageAttachmentDataUrl,
47-
convertPastedImagesToAttachments,
48-
isOwnedPastedImageUri,
49-
toUploadChatImageAttachments,
50-
} from "./composerImages";
51-
52-
describe("toUploadChatImageAttachments", () => {
53-
beforeEach(() => {
54-
files.clear();
55-
paths.documentUri = "file:///documents";
56-
});
57-
58-
it("strips client draft id and previewUri for the startTurn wire shape", async () => {
59-
await expect(
60-
toUploadChatImageAttachments([
61-
{
62-
id: "client-draft-id",
63-
type: "image",
64-
name: "pasted-image.png",
65-
mimeType: "image/png",
66-
sizeBytes: 12,
67-
dataUrl: "data:image/png;base64,AA==",
68-
previewUri: "file:///tmp/preview.png",
69-
},
70-
]),
71-
).resolves.toEqual([
72-
{
73-
type: "image",
74-
name: "pasted-image.png",
75-
mimeType: "image/png",
76-
sizeBytes: 12,
77-
dataUrl: "data:image/png;base64,AA==",
78-
},
79-
]);
80-
});
81-
82-
it("reads file-backed image bytes lazily from the owned copy", async () => {
83-
const fileUri = "file:///documents/t3-composer-attachments/attachment-id-photo.png";
84-
files.set(fileUri, { base64: "aGVsbG8=", deleted: false });
85-
86-
await expect(
87-
toUploadChatImageAttachments([
88-
{
89-
id: "client-draft-id",
90-
type: "image",
91-
name: "photo.png",
92-
mimeType: "image/png",
93-
sizeBytes: 5,
94-
fileUri,
95-
previewUri: fileUri,
96-
},
97-
]),
98-
).resolves.toEqual([
99-
{
100-
type: "image",
101-
name: "photo.png",
102-
mimeType: "image/png",
103-
sizeBytes: 5,
104-
dataUrl: "data:image/png;base64,aGVsbG8=",
105-
},
106-
]);
107-
});
108-
109-
it("resolves a restored image from the current iOS document container", async () => {
110-
const fileName = "33333333-3333-4333-8333-333333333333-photo.png";
111-
paths.documentUri =
112-
"file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents";
113-
const currentUri = `${paths.documentUri}/t3-composer-attachments/${fileName}`;
114-
files.set(currentUri, { base64: "aGVsbG8=", deleted: false });
115-
116-
await expect(
117-
composerImageAttachmentDataUrl({
118-
id: "client-draft-id",
119-
type: "image",
120-
name: "photo.png",
121-
mimeType: "image/png",
122-
sizeBytes: 5,
123-
fileUri: `file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`,
124-
previewUri: currentUri,
125-
}),
126-
).resolves.toBe("data:image/png;base64,aGVsbG8=");
127-
});
128-
129-
it("rejects an attachment that lost both its bytes and its file", async () => {
130-
await expect(
131-
composerImageAttachmentDataUrl({
132-
id: "client-draft-id",
133-
type: "image",
134-
name: "photo.png",
135-
mimeType: "image/png",
136-
sizeBytes: 5,
137-
previewUri: "file:///tmp/preview.png",
138-
}),
139-
).rejects.toThrow("'photo.png' is no longer available. Attach the image again.");
140-
});
141-
});
39+
import { convertPastedImagesToAttachments, isOwnedPastedImageUri } from "./composerImages";
14240

14341
describe("native pasted image cleanup", () => {
14442
beforeEach(() => {

‎apps/mobile/src/lib/composerImages.ts‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,43 +54,6 @@ export function isFileBackedComposerAttachment(
5454
return attachment.fileUri !== undefined;
5555
}
5656

57-
/**
58-
* Wire shape for startTurn on servers without attachment uploads: pure inline
59-
* uploads without client draft id / previewUri. File-backed images read their
60-
* base64 from disk lazily, only when this legacy path is actually taken.
61-
*/
62-
export async function toUploadChatImageAttachments(
63-
attachments: ReadonlyArray<DraftComposerImageAttachment>,
64-
): Promise<ReadonlyArray<UploadChatImageAttachment>> {
65-
return Promise.all(
66-
attachments.map(async (attachment) => ({
67-
type: attachment.type,
68-
name: attachment.name,
69-
mimeType: attachment.mimeType,
70-
sizeBytes: attachment.sizeBytes,
71-
dataUrl: await composerImageAttachmentDataUrl(attachment),
72-
})),
73-
);
74-
}
75-
76-
/** Inline bytes for one image: legacy drafts carry them, file-backed ones read them from disk. */
77-
export async function composerImageAttachmentDataUrl(
78-
attachment: DraftComposerImageAttachment,
79-
): Promise<string> {
80-
if (attachment.dataUrl !== undefined) {
81-
return attachment.dataUrl;
82-
}
83-
if (attachment.fileUri === undefined) {
84-
throw new Error(`'${attachment.name}' is no longer available. Attach the image again.`);
85-
}
86-
const { File, Paths } = await import("expo-file-system");
87-
const uri =
88-
resolveOwnedComposerAttachmentFileUri(attachment.fileUri, Paths.document.uri) ??
89-
attachment.fileUri;
90-
const base64 = await new File(uri).base64();
91-
return `data:${attachment.mimeType};base64,${base64}`;
92-
}
93-
9457
const OWNED_PASTED_IMAGE_DIRECTORY = "t3-composer-paste";
9558
const ATTACHMENT_COPY_CHUNK_BYTES = 64 * 1024;
9659

0 commit comments

Comments
 (0)