Skip to content

Commit 5a157c0

Browse files
StiensWoutclaude
andcommitted
perf(clients): fix composer draft persistence boundaries
Web: the zustand persist pipeline ran the full draft walk plus JSON.stringify on every store write, so each keystroke serialized every persisted draft, including base64 image attachments; only the final localStorage write was debounced. partialize now captures the live state cheaply and the walk + stringify run once per idle flush inside the debounced storage. 200 keystrokes next to two 1 MB images: 200 serializations / 534.6 MB / 1693.5 ms of main-thread time down to 1 / 2.7 MB / 6.8 ms. Mobile: picked and pasted images persisted full base64 data URLs into drafts.json, thread-outbox/*.json, and incoming-shares/*.json (an 8 MB image cost 21.3 MB of JSON per copy, rewritten on every 200 ms draft debounce). Images are now file-backed like file attachments already were: bytes are copied once into the app-owned attachment directory and drafts persist metadata plus fileUri (322 bytes). Uploads stream from the owned file; base64 is materialized lazily only for the old-server inline fallback. Legacy dataUrl drafts still hydrate, preview, upload, and send. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent b883fc0 commit 5a157c0

22 files changed

Lines changed: 762 additions & 256 deletions

‎apps/mobile/src/components/ComposerAttachmentStrip.tsx‎

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { useEffect, useRef, useState } from "react";
44
import { Alert, Image, Pressable, ScrollView, View } from "react-native";
55

66
import { AppText as Text } from "./AppText";
7-
import type { DraftComposerAttachment, DraftComposerFileAttachment } from "../lib/composerImages";
7+
import {
8+
isFileBackedComposerAttachment,
9+
type DraftComposerAttachment,
10+
type DraftComposerFileAttachment,
11+
type DraftComposerImageAttachment,
12+
} from "../lib/composerImages";
13+
import { resolveOwnedComposerAttachmentFileUri } from "../lib/composerAttachmentFiles";
814
import { VideoAttachmentTile } from "./VideoAttachmentTile";
915
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
1016
import { PresentationSource } from "./NativePresentation";
@@ -87,35 +93,75 @@ export function ComposerAttachmentThumbnail(props: ComposerAttachmentThumbnailPr
8793
);
8894
}
8995

96+
/**
97+
* Thumbnail URI for a draft image. File-backed previews rebase into the
98+
* current iOS data container (its UUID changes across installs); the raw
99+
* persisted URI renders meanwhile, which is correct everywhere but after a
100+
* container move.
101+
*/
102+
function useComposerImagePreviewUri(attachment: DraftComposerImageAttachment): string {
103+
const { fileUri, previewUri } = attachment;
104+
const [rebased, setRebased] = useState<{ fileUri: string; uri: string } | null>(null);
105+
useEffect(() => {
106+
if (fileUri === undefined) return;
107+
let cancelled = false;
108+
void (async () => {
109+
const { Paths } = await import("expo-file-system");
110+
const owned = resolveOwnedComposerAttachmentFileUri(fileUri, Paths.document.uri);
111+
// Re-render only when the container actually moved.
112+
if (!cancelled && owned !== null && owned !== previewUri) setRebased({ fileUri, uri: owned });
113+
})();
114+
return () => {
115+
cancelled = true;
116+
};
117+
}, [fileUri, previewUri]);
118+
return fileUri !== undefined && rebased?.fileUri === fileUri ? rebased.uri : previewUri;
119+
}
120+
121+
function ComposerImageAttachment(
122+
props: ComposerAttachmentThumbnailProps & { readonly attachment: DraftComposerImageAttachment },
123+
) {
124+
const { attachment } = props;
125+
const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
126+
const previewUri = useComposerImagePreviewUri(attachment);
127+
const sourceIdentifier = `draft-image:${attachment.id}`;
128+
return (
129+
<PresentationSource identifier={sourceIdentifier}>
130+
<Pressable
131+
accessibilityRole="imagebutton"
132+
accessibilityLabel={`Open ${attachment.name}`}
133+
disabled={!props.onPressPreview}
134+
onPress={() =>
135+
props.onPressPreview?.(
136+
// File-backed images open through the retain-lease + container
137+
// rebase path; legacy drafts still carry their inline bytes.
138+
isFileBackedComposerAttachment(attachment)
139+
? { kind: "image", attachment, name: attachment.name, sourceIdentifier }
140+
: {
141+
kind: "image",
142+
uri: attachment.dataUrl ?? attachment.previewUri,
143+
name: attachment.name,
144+
sourceIdentifier,
145+
},
146+
)
147+
}
148+
>
149+
<Image
150+
source={{ uri: previewUri }}
151+
style={style}
152+
className="bg-subtle"
153+
resizeMode="cover"
154+
/>
155+
</Pressable>
156+
</PresentationSource>
157+
);
158+
}
159+
90160
function ComposerAttachmentContent(props: ComposerAttachmentThumbnailProps) {
91161
const { attachment } = props;
92162
const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
93163
if (attachment.type === "image") {
94-
const sourceIdentifier = `draft-image:${attachment.id}`;
95-
return (
96-
<PresentationSource identifier={sourceIdentifier}>
97-
<Pressable
98-
accessibilityRole="imagebutton"
99-
accessibilityLabel={`Open ${attachment.name}`}
100-
disabled={!props.onPressPreview}
101-
onPress={() =>
102-
props.onPressPreview?.({
103-
kind: "image",
104-
uri: attachment.dataUrl,
105-
name: attachment.name,
106-
sourceIdentifier,
107-
})
108-
}
109-
>
110-
<Image
111-
source={{ uri: attachment.previewUri }}
112-
style={style}
113-
className="bg-subtle"
114-
resizeMode="cover"
115-
/>
116-
</Pressable>
117-
</PresentationSource>
118-
);
164+
return <ComposerImageAttachment {...props} attachment={attachment} />;
119165
}
120166
const onPressVideo = props.onPressVideo;
121167
if (onPressVideo && videoMimeType(attachment) !== null) {

‎apps/mobile/src/components/FilePreviewModal.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
33
import { useEffect, useEffectEvent, useState } from "react";
44
import { Alert, Keyboard } from "react-native";
55

6-
import type { DraftComposerFileAttachment } from "../lib/composerImages";
6+
import type { FileBackedComposerAttachment } from "../lib/composerImages";
77
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
88
import { useAssetUrlState } from "../state/assets";
99
import { usePreparedConnection } from "../state/session";
@@ -19,7 +19,7 @@ export interface ResolvedFilePreviewSource {
1919
export type FilePreviewSource = Omit<ResolvedFilePreviewSource, "uri"> &
2020
(
2121
| { readonly uri: string }
22-
| { readonly attachment: DraftComposerFileAttachment }
22+
| { readonly attachment: FileBackedComposerAttachment }
2323
| { readonly environmentId: EnvironmentId; readonly resource: AssetResource }
2424
);
2525

‎apps/mobile/src/features/sharing/incoming-share-model.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ describe("incoming native shares", () => {
3636
originalName: "Screenshot.png",
3737
};
3838
const removeOwnedFile = vi.fn(() => Promise.resolve());
39+
const readBase64 = vi.fn(async () => "unused");
40+
const persistedUri = "file:///documents/t3-composer-attachments/Screenshot.png";
41+
const persistFile = vi.fn(async () => persistedUri);
3942

4043
const result = await buildIncomingShareDraft({
4144
id: "share-1",
4245
createdAt: "2026-07-15T10:00:00.000Z",
4346
payloads,
4447
resolvedPayloads: [resolvedImage],
4548
fileReader: {
46-
readBase64: async () => "YWJj",
49+
readBase64,
50+
persistFile,
51+
readSize: async (uri) => (uri === persistedUri ? 3 : null),
4752
removeOwnedFile,
4853
},
4954
});
@@ -60,13 +65,16 @@ describe("incoming native shares", () => {
6065
name: "Screenshot.png",
6166
mimeType: "image/png",
6267
sizeBytes: 3,
63-
dataUrl: "data:image/png;base64,YWJj",
64-
previewUri: "data:image/png;base64,YWJj",
68+
fileUri: persistedUri,
69+
previewUri: persistedUri,
6570
},
6671
],
6772
warnings: [],
6873
});
74+
expect(persistFile).toHaveBeenCalledWith(image.value, "Screenshot.png");
75+
expect(readBase64).not.toHaveBeenCalled();
6976
expect(removeOwnedFile).toHaveBeenCalledWith(image.value);
77+
expect(removeOwnedFile).not.toHaveBeenCalledWith(persistedUri);
7078
expect(hasIncomingShareContent(result)).toBe(true);
7179
});
7280

‎apps/mobile/src/features/sharing/incoming-share-model.ts‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,29 +405,61 @@ export async function buildIncomingShareDraft(input: {
405405
continue;
406406
}
407407

408+
const imageName = resolved?.originalName ?? fallbackName(uri, index, mimeType);
409+
let persistedImageUri: string | undefined;
408410
try {
411+
if (input.fileReader.persistFile) {
412+
// The share provider's file is temporary. Copy it into the durable
413+
// attachment directory so the composer stays valid after the source
414+
// file and App Group entry are gone, without inlining the bytes.
415+
persistedImageUri = await input.fileReader.persistFile(uri, imageName);
416+
const sizeBytes =
417+
(await input.fileReader.readSize?.(persistedImageUri)) ?? resolved?.contentSize ?? null;
418+
if (sizeBytes === null || sizeBytes <= 0) {
419+
warnings.push(`'${imageName}' is empty or could not be read.`);
420+
await releaseOwnedFiles(input.fileReader, [persistedImageUri]);
421+
continue;
422+
}
423+
if (sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
424+
warnings.push(`'${imageName}' exceeds the 10 MB attachment limit.`);
425+
await releaseOwnedFiles(input.fileReader, [persistedImageUri]);
426+
continue;
427+
}
428+
attachments.push({
429+
id: `${input.id}:image:${index}`,
430+
type: "image",
431+
name: imageName,
432+
mimeType,
433+
sizeBytes,
434+
fileUri: persistedImageUri,
435+
previewUri: persistedImageUri,
436+
});
437+
continue;
438+
}
439+
// Readers without persistFile keep the legacy inline shape.
409440
const base64 = await input.fileReader.readBase64(uri);
410441
const sizeBytes = resolved?.contentSize ?? estimateBase64ByteSize(base64);
411442
if (sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
412-
warnings.push(
413-
`'${resolved?.originalName ?? fallbackName(uri, index, mimeType)}' exceeds the 10 MB attachment limit.`,
414-
);
443+
warnings.push(`'${imageName}' exceeds the 10 MB attachment limit.`);
415444
continue;
416445
}
417446
const dataUrl = `data:${mimeType};base64,${base64}`;
418447
attachments.push({
419448
id: `${input.id}:image:${index}`,
420449
type: "image",
421-
name: resolved?.originalName ?? fallbackName(uri, index, mimeType),
450+
name: imageName,
422451
mimeType,
423452
sizeBytes,
424453
dataUrl,
425-
// The share provider's file is temporary. A data-backed preview keeps
426-
// the composer valid after its source file and App Group entry are gone.
427454
previewUri: dataUrl,
428455
});
429456
} catch {
430457
warnings.push(`Could not read '${fallbackName(uri, index, mimeType)}'.`);
458+
// A copy persisted before the failure has no attachment referencing it;
459+
// release it or it leaks in the app's attachment directory.
460+
if (persistedImageUri !== undefined) {
461+
await releaseOwnedFiles(input.fileReader, [persistedImageUri]);
462+
}
431463
} finally {
432464
await releaseOwnedFiles(input.fileReader, [uri, payload.value]);
433465
}

‎apps/mobile/src/features/threads/use-project-actions.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export function useCreateProjectThread() {
120120
messageId: metadata.messageId,
121121
createdAt: metadata.createdAt,
122122
text: initialMessageText,
123-
attachments: input.initialAttachments,
124123
uploadedAttachments: prepared.attachments,
125124
modelSelection: input.modelSelection,
126125
runtimeMode: input.runtimeMode,

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const mocks = vi.hoisted(() => ({
1414
upload: vi.fn(),
1515
writeFile: vi.fn(),
1616
deleteFile: vi.fn(),
17+
readBase64: vi.fn(),
1718
}));
1819

1920
vi.mock("@t3tools/client-runtime/state/runtime", () => ({
@@ -68,6 +69,10 @@ vi.mock("expo-file-system", () => ({
6869
mocks.deleteFile(this.uri);
6970
}
7071

72+
async base64() {
73+
return mocks.readBase64(this.uri);
74+
}
75+
7176
upload(url: string, options: unknown) {
7277
return mocks.upload(this.uri, url, options);
7378
}
@@ -102,6 +107,16 @@ const image = {
102107
previewUri: "file:///images/screenshot.png",
103108
} as const satisfies DraftComposerAttachment;
104109

110+
const fileBackedImage = {
111+
id: "image-2",
112+
type: "image",
113+
name: "photo.png",
114+
mimeType: "image/png",
115+
sizeBytes: 3,
116+
fileUri: "file:///documents/t3-composer-attachments/photo.png",
117+
previewUri: "file:///documents/t3-composer-attachments/photo.png",
118+
} as const satisfies DraftComposerAttachment;
119+
105120
const file = {
106121
id: "file-1",
107122
type: "file",
@@ -174,6 +189,8 @@ describe("prepareTurnAttachments", () => {
174189
mocks.upload.mockReset();
175190
mocks.writeFile.mockReset();
176191
mocks.deleteFile.mockReset();
192+
mocks.readBase64.mockReset();
193+
mocks.readBase64.mockResolvedValue("YWJj");
177194
mocks.readAtom.mockReturnValue(Option.some({ httpBaseUrl: "https://environment.example/" }));
178195
mocks.runAtomCommand.mockImplementation(async (_registry: unknown, command: unknown) =>
179196
command === mocks.createUploadUrl
@@ -208,6 +225,55 @@ describe("prepareTurnAttachments", () => {
208225
expect(mocks.upload).not.toHaveBeenCalled();
209226
});
210227

228+
it("inlines a file-backed image lazily when the server lacks image uploads", async () => {
229+
const prepared = await prepareTurnAttachments({
230+
environmentId,
231+
attachments: [fileBackedImage],
232+
});
233+
234+
expect(prepared.status).toBe("ready");
235+
if (prepared.status !== "ready") return;
236+
expect(prepared.attachments).toEqual([
237+
{
238+
type: "image",
239+
name: "photo.png",
240+
mimeType: "image/png",
241+
sizeBytes: 3,
242+
dataUrl: "data:image/png;base64,YWJj",
243+
},
244+
]);
245+
expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith(fileBackedImage.fileUri);
246+
expect(mocks.upload).not.toHaveBeenCalled();
247+
});
248+
249+
it("uploads a file-backed image from its owned copy without staging base64", async () => {
250+
const prepared = await prepareTurnAttachments({
251+
environmentId,
252+
attachments: [fileBackedImage],
253+
supportsImageUploads: true,
254+
});
255+
256+
expect(mocks.upload).toHaveBeenCalledWith(
257+
fileBackedImage.fileUri,
258+
"https://environment.example/api/attachments/upload/signed",
259+
expect.objectContaining({ headers: { "Content-Type": "image/png" } }),
260+
);
261+
expect(mocks.readBase64).not.toHaveBeenCalled();
262+
expect(mocks.writeFile).not.toHaveBeenCalled();
263+
expect(mocks.deleteFile).not.toHaveBeenCalled();
264+
expect(prepared.status).toBe("ready");
265+
if (prepared.status !== "ready") return;
266+
expect(prepared.attachments).toEqual([
267+
{
268+
type: "image",
269+
id: MINTED_ID,
270+
name: "photo.png",
271+
mimeType: "image/png",
272+
sizeBytes: 3,
273+
},
274+
]);
275+
});
276+
211277
it("uploads generic file bytes directly and keeps mixed attachment order", async () => {
212278
const prepared = await prepareTurnAttachments({ environmentId, attachments: [file, image] });
213279

0 commit comments

Comments
 (0)