From c7f5e238a66d2aa1ddcb6d4c6ff686938da725c5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 10:09:58 -0800 Subject: [PATCH 1/5] refactor(mobile): one shared upload per composer attachment Composer attachment uploads run in an Effect RcMap keyed by environment and attachment. The composer holds the uploads it shows or has queued, and sends (outbox delivery, queued-run edits) join the running upload instead of starting their own, so an attachment can no longer upload twice whatever order subscribers run in. An upload is interrupted once nothing holds it, and is ready only after its id is saved on the drafts and queued messages that hold it. Co-Authored-By: Claude Opus 5.5 --- apps/mobile/src/lib/attachmentUpload.test.ts | 35 ++- apps/mobile/src/lib/attachmentUpload.ts | 210 ++++++++--------- .../lib/composerAttachmentUploadQueue.test.ts | 69 ++++-- .../src/lib/composerAttachmentUploadQueue.ts | 223 ++++++++++++------ .../state/composer-attachment-uploads.test.ts | 3 +- .../src/state/composer-attachment-uploads.ts | 116 +++++---- .../threadOutboxDelivery.permissions.test.ts | 1 + .../src/state/threadOutboxDelivery.test.ts | 1 + apps/mobile/src/state/threadOutboxDelivery.ts | 3 + .../src/state/use-composer-drafts.test.ts | 2 + .../src/state/use-thread-composer-state.ts | 3 + 11 files changed, 405 insertions(+), 261 deletions(-) diff --git a/apps/mobile/src/lib/attachmentUpload.test.ts b/apps/mobile/src/lib/attachmentUpload.test.ts index 0097ab7ed77c0..cba8f1566717a 100644 --- a/apps/mobile/src/lib/attachmentUpload.test.ts +++ b/apps/mobile/src/lib/attachmentUpload.test.ts @@ -90,10 +90,32 @@ vi.mock("expo-file-system", () => ({ })); import { - prepareTurnAttachments, + prepareTurnAttachments as prepareWithUploads, releasePendingAttachmentUploads, + uploadComposerAttachment, withUploadedMobileAttachmentReferences, } from "./attachmentUpload"; + +/** Uploads each attachment directly, as the shared registry does for one holder. */ +function prepareTurnAttachments( + input: Omit[0], "uploadAttachment"> & { + readonly onUploadProgress?: (attachmentId: string, progress: number) => void; + }, +) { + const signal = input.signal ?? new AbortController().signal; + return prepareWithUploads({ + ...input, + uploadAttachment: (attachment) => + uploadComposerAttachment( + input.environmentId, + attachment, + signal, + input.onUploadProgress + ? (progress) => input.onUploadProgress?.(attachment.id, progress) + : undefined, + ), + }); +} import type { DraftComposerAttachment } from "./composerImages"; const environmentId = EnvironmentId.make("environment-1"); @@ -731,17 +753,6 @@ describe("prepareTurnAttachments", () => { expect(removeCallsFor("pending-existing-pdf")).toBe(0); }); - - it("deletes the minted uploads when the owner abandons the send", async () => { - const result = await prepareTurnAttachments({ - environmentId, - attachments: [file], - persistUploadedReferences: async () => "abandon", - }); - - expect(result.status).toBe("abandoned"); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); }); describe("releasePendingAttachmentUploads", () => { diff --git a/apps/mobile/src/lib/attachmentUpload.ts b/apps/mobile/src/lib/attachmentUpload.ts index 57699af22819e..b129b840370d2 100644 --- a/apps/mobile/src/lib/attachmentUpload.ts +++ b/apps/mobile/src/lib/attachmentUpload.ts @@ -248,13 +248,90 @@ async function uploadFileBytes( } } -/** persistUploadedReferences runs only for new uploaded ids; save them to their owner so crash retries reuse bytes. */ +function requireUploadAccess(environmentId: EnvironmentId): void { + if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) { + throw new Error("This connection cannot upload attachments."); + } +} + +/** + * Uploads one attachment and resolves to its uploaded id, reusing an earlier upload the server + * still has. An abort deletes the pending upload it created. + */ +export async function uploadComposerAttachment( + environmentId: EnvironmentId, + attachment: DraftComposerAttachment, + signal: AbortSignal, + onProgress?: (progress: number) => void, +): Promise { + requireUploadAccess(environmentId); + if ( + attachment.uploadEnvironmentId === environmentId && + attachment.uploadedAttachmentId !== undefined + ) { + const verification = await verifyPersistedAttachmentUpload({ + registry: appAtomRegistry, + createAssetUrl: assetEnvironment.createUrl, + environmentId, + attachmentId: attachment.uploadedAttachmentId, + }); + if (verification.status === "failed") throw verification.error; + if (verification.status === "verified") return attachment.uploadedAttachmentId; + } + if (signal.aborted) throw new Error("Upload cancelled."); + requireUploadAccess(environmentId); + if ( + Option.isNone( + appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)), + ) + ) { + throw new Error("The environment is not connected."); + } + let minted: string | null = null; + const result = await runAttachmentUploadCycle({ + registry: appAtomRegistry, + createUploadUrl: attachmentEnvironment.createUploadUrl, + remove: attachmentEnvironment.remove, + environmentId, + upload: attachmentUploadInput(attachment), + // Resolve at transfer time; reconnects may change the base URL. + resolveUploadUrl: (relativeUrl) => { + requireUploadAccess(environmentId); + const connection = appAtomRegistry.get( + environmentSession.preparedConnectionValueAtom(environmentId), + ); + return Option.isNone(connection) + ? null + : resolveAssetUrl(connection.value.httpBaseUrl, relativeUrl); + }, + transport: (url) => ({ + done: uploadFileBytes(environmentId, attachment, url, signal, onProgress), + abort: () => {}, + }), + onMinted: (attachmentId) => { + if (signal.aborted) return "cancel"; + minted = attachmentId; + return "continue"; + }, + }); + if (result.status === "uploaded") return result.attachmentId; + if (minted !== null) await releaseCreatedUploadsQuietly(environmentId, [minted]); + throw result.status === "failed" && result.error !== undefined + ? result.error + : new Error(`Upload failed for '${attachment.name}'.`); +} + +/** + * Turns a message's attachments into wire references. `uploadAttachment` is the shared upload + * registry, so a send joins an upload the composer already started. persistUploadedReferences + * runs only for new uploaded ids; save them to their owner so crash retries reuse bytes. + */ export async function prepareTurnAttachments(input: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; readonly supportsImageUploads?: boolean; readonly signal?: AbortSignal; - readonly onUploadProgress?: (attachmentId: string, progress: number) => void; + readonly uploadAttachment: (attachment: DraftComposerAttachment) => Promise; readonly persistUploadedReferences?: ( draftAttachments: ReadonlyArray, ) => Promise<"persisted" | "abandon">; @@ -286,121 +363,40 @@ export async function prepareTurnAttachments(input: { } } - const requireUploadAccess = () => { - if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) { - throw new Error("This connection cannot upload attachments."); - } - }; - requireUploadAccess(); - - const connection = appAtomRegistry.get( - environmentSession.preparedConnectionValueAtom(environmentId), - ); - if (Option.isNone(connection)) { - throw new Error("The environment is not connected."); - } - + requireUploadAccess(environmentId); const uploadedAttachments: UploadedMobileAttachment[] = []; const pendingAttachmentIds: string[] = []; - const createdAttachmentIds: string[] = []; - const controller = new AbortController(); - const abort = () => controller.abort(); - input.signal?.addEventListener("abort", abort, { once: true }); try { for (const attachment of input.attachments) { - if (controller.signal.aborted) throw new Error("Upload cancelled."); - requireUploadAccess(); + if (input.signal?.aborted) return { status: "abandoned" }; if (attachment.type === "image" && !input.supportsImageUploads) { uploadedAttachments.push(...(await toUploadChatImageAttachments([attachment]))); continue; } - - if ( - attachment.uploadEnvironmentId === environmentId && - attachment.uploadedAttachmentId !== undefined - ) { - const verification = await verifyPersistedAttachmentUpload({ - registry: appAtomRegistry, - createAssetUrl: assetEnvironment.createUrl, - environmentId, - attachmentId: attachment.uploadedAttachmentId, - }); - if (verification.status === "failed") { - throw verification.error; - } - if (verification.status === "verified") { - pendingAttachmentIds.push(attachment.uploadedAttachmentId); - uploadedAttachments.push(uploadedReference(attachment, attachment.uploadedAttachmentId)); - continue; - } - } - - requireUploadAccess(); - const result = await runAttachmentUploadCycle({ - registry: appAtomRegistry, - createUploadUrl: attachmentEnvironment.createUploadUrl, - remove: attachmentEnvironment.remove, - environmentId, - upload: attachmentUploadInput(attachment), - // Resolve at transfer time; reconnects may change the base URL. - resolveUploadUrl: (relativeUrl) => { - requireUploadAccess(); - const currentConnection = appAtomRegistry.get( - environmentSession.preparedConnectionValueAtom(environmentId), - ); - return Option.isNone(currentConnection) - ? null - : resolveAssetUrl(currentConnection.value.httpBaseUrl, relativeUrl); - }, - transport: (url) => ({ - done: uploadFileBytes( - environmentId, - attachment, - url, - controller.signal, - input.onUploadProgress - ? (progress) => input.onUploadProgress?.(attachment.id, progress) - : undefined, - ), - abort, - }), - onMinted: (attachmentId) => { - if (controller.signal.aborted) return "cancel"; - pendingAttachmentIds.push(attachmentId); - createdAttachmentIds.push(attachmentId); - return "continue"; - }, - }); - if (result.status !== "uploaded") { - throw result.status === "failed" && result.error !== undefined - ? result.error - : new Error(`Upload failed for '${attachment.name}'.`); - } - uploadedAttachments.push(uploadedReference(attachment, result.attachmentId)); + const uploadedId = await input.uploadAttachment(attachment); + pendingAttachmentIds.push(uploadedId); + uploadedAttachments.push(uploadedReference(attachment, uploadedId)); } - - if (controller.signal.aborted) throw new Error("Upload cancelled."); - - const draftAttachments = withUploadedMobileAttachmentReferences({ - environmentId, - attachments: input.attachments, - uploadedAttachments, - }); - const referencesChanged = draftAttachments.some( - (attachment, index) => attachment !== input.attachments[index], - ); - if (referencesChanged && input.persistUploadedReferences) { - if ((await input.persistUploadedReferences(draftAttachments)) === "abandon") { - await releaseCreatedUploadsQuietly(environmentId, createdAttachmentIds); - return { status: "abandoned" }; - } - } - return ready(uploadedAttachments, pendingAttachmentIds, draftAttachments); } catch (error) { - await releaseCreatedUploadsQuietly(environmentId, createdAttachmentIds); - if (controller.signal.aborted) return { status: "abandoned" }; + if (input.signal?.aborted) return { status: "abandoned" }; throw error; - } finally { - input.signal?.removeEventListener("abort", abort); } + if (input.signal?.aborted) return { status: "abandoned" }; + + const draftAttachments = withUploadedMobileAttachmentReferences({ + environmentId, + attachments: input.attachments, + uploadedAttachments, + }); + const referencesChanged = draftAttachments.some( + (attachment, index) => attachment !== input.attachments[index], + ); + if ( + referencesChanged && + input.persistUploadedReferences && + (await input.persistUploadedReferences(draftAttachments)) === "abandon" + ) { + return { status: "abandoned" }; + } + return ready(uploadedAttachments, pendingAttachmentIds, draftAttachments); } diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts index 6de39c413d49e..56cfa301df861 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts @@ -33,7 +33,7 @@ function request(id: string, environment = environmentId): ComposerAttachmentUpl describe("composer attachment upload queue", () => { it("bounds concurrency, deduplicates updates, and drains all attachments", async () => { - const gates = new Map>>(); + const gates = new Map>>(); const fourthStarted = Promise.withResolvers(); const firstThreeStarted = Promise.withResolvers(); let active = 0; @@ -41,7 +41,7 @@ describe("composer attachment upload queue", () => { const upload = vi.fn(async (input: ComposerAttachmentUploadRequest) => { active += 1; maximum = Math.max(maximum, active); - const gate = Promise.withResolvers(); + const gate = Promise.withResolvers(); gates.set(input.attachment.id, gate); if (gates.size === 3) firstThreeStarted.resolve(); if (gates.size === 4) fourthStarted.resolve(); @@ -57,9 +57,9 @@ describe("composer attachment upload queue", () => { queue.sync(requests); await firstThreeStarted.promise; expect(upload).toHaveBeenCalledTimes(3); - gates.get("one")!.resolve(true); + gates.get("one")!.resolve("uploaded"); await fourthStarted.promise; - for (const gate of gates.values()) gate.resolve(true); + for (const gate of gates.values()) gate.resolve("uploaded"); await queue.settled(); queue.sync(requests); await queue.settled(); @@ -76,8 +76,8 @@ describe("composer attachment upload queue", () => { async (_request: ComposerAttachmentUploadRequest, currentSignal: AbortSignal) => { signal = currentSignal; started.resolve(); - return new Promise((resolve) => - currentSignal.addEventListener("abort", () => resolve(false), { once: true }), + return new Promise((resolve) => + currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }), ); }, ); @@ -94,7 +94,7 @@ describe("composer attachment upload queue", () => { await queue.settled(); expect(signal?.aborted).toBe(true); expect(states).toEqual({}); - upload.mockResolvedValueOnce(true); + upload.mockResolvedValueOnce("uploaded"); queue.sync([local]); await queue.settled(); expect(upload.mock.calls[1]?.[0]).toBe(local); @@ -107,7 +107,10 @@ describe("composer attachment upload queue", () => { it("retries a failed attachment when the worker restores requests after reconnect", async () => { let states: Readonly> = {}; - const upload = vi.fn().mockRejectedValueOnce(new Error("Disconnected")).mockResolvedValue(true); + const upload = vi + .fn() + .mockRejectedValueOnce(new Error("Disconnected")) + .mockResolvedValue("uploaded"); const queue = createComposerAttachmentUploadQueue({ upload, onChange: (next) => { @@ -131,7 +134,7 @@ describe("composer attachment upload queue", () => { }); it("ignores a late completion after removal or environment switch", async () => { - const gate = Promise.withResolvers(); + const gate = Promise.withResolvers(); const started = Promise.withResolvers(); let states: Readonly> = {}; const upload = vi.fn(async () => { @@ -146,10 +149,10 @@ describe("composer attachment upload queue", () => { }); queue.sync([request("photo")]); await started.promise; - upload.mockResolvedValueOnce(true); + upload.mockResolvedValueOnce("uploaded"); const other = EnvironmentId.make("environment-2"); queue.sync([request("photo", other)]); - gate.resolve(true); + gate.resolve("uploaded"); await queue.settled(); expect(states).toEqual({ [composerAttachmentUploadKey(other, "photo")]: { status: "ready" } }); queue.sync([]); @@ -159,9 +162,9 @@ describe("composer attachment upload queue", () => { it("restarts a re-added attachment after its aborted transfer finishes settling", async () => { const firstStarted = Promise.withResolvers(); - const firstSettled = Promise.withResolvers(); + const firstSettled = Promise.withResolvers(); const secondStarted = Promise.withResolvers(); - const secondSettled = Promise.withResolvers(); + const secondSettled = Promise.withResolvers(); let states: Readonly> = {}; let firstSignal: AbortSignal | undefined; const upload = vi.fn(async (_request: ComposerAttachmentUploadRequest, signal: AbortSignal) => { @@ -186,10 +189,10 @@ describe("composer attachment upload queue", () => { queue.sync([local]); expect(firstSignal?.aborted).toBe(true); expect(upload).toHaveBeenCalledOnce(); - firstSettled.resolve(false); + firstSettled.resolve("aborted"); await secondStarted.promise; expect(upload).toHaveBeenCalledTimes(2); - secondSettled.resolve(true); + secondSettled.resolve("uploaded"); await queue.settled(); expect(states[composerAttachmentUploadKey(environmentId, local.attachment.id)]).toEqual({ status: "ready", @@ -205,7 +208,7 @@ describe("composer attachment upload queue", () => { _request: ComposerAttachmentUploadRequest, _signal: AbortSignal, report: (value: number) => void, - ): Promise => { + ): Promise => { report(0.12); report(0.13); report(1.1); @@ -229,19 +232,41 @@ describe("composer attachment upload queue", () => { reason: "Server unavailable", }); expect(progress).toEqual([0, 0.1, 1]); - upload.mockImplementationOnce(async () => true); + upload.mockImplementationOnce(async () => "uploaded"); queue.retry(environmentId, "file"); await queue.settled(); expect(states[composerAttachmentUploadKey(environmentId, "file")]).toEqual({ status: "ready" }); queue.dispose(); }); - it("does not spin when an upload's draft was abandoned before persistence", async () => { - const upload = vi.fn(async () => false); - const queue = createComposerAttachmentUploadQueue({ upload, onChange: () => {} }); - queue.sync([request("discarded")]); - await queue.settled(); + it("joins a send to the running upload instead of uploading again", async () => { + const gate = Promise.withResolvers(); + const started = Promise.withResolvers(); + const uploaded = vi.fn(); + const upload = vi.fn(async () => { + started.resolve(); + return gate.promise; + }); + let states: Readonly> = {}; + const queue = createComposerAttachmentUploadQueue({ + upload, + onUploaded: uploaded, + onChange: (next) => { + states = next; + }, + }); + const local = request("video"); + queue.sync([local]); + await started.promise; + // Sending hands the attachment from the draft to the outbox, which uploads it too. + const sent = queue.upload(local); + queue.sync([]); + gate.resolve("uploaded-1"); + await expect(sent).resolves.toBe("uploaded-1"); expect(upload).toHaveBeenCalledOnce(); + expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); + await queue.settled(); + expect(states).toEqual({}); queue.dispose(); }); }); diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index 0a28fff2231d5..fe3836ff4c256 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -1,4 +1,13 @@ import { EnvironmentId, type ServerConfig } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Equal from "effect/Equal"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import * as Hash from "effect/Hash"; +import * as RcMap from "effect/RcMap"; +import * as Schema from "effect/Schema"; +import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments"; import { parseLegacyNewTaskDraftKey } from "../state/new-task-draft-key"; @@ -129,22 +138,45 @@ export function queuedAttachmentsStillUploading(input: { ); } +/** Equal by environment and attachment id; the request rides along for the upload. */ +class UploadKey implements Equal.Equal { + constructor( + readonly id: string, + readonly request: ComposerAttachmentUploadRequest | null, + ) {} + static of(request: ComposerAttachmentUploadRequest): UploadKey { + return new UploadKey( + composerAttachmentUploadKey(request.environmentId, request.attachment.id), + request, + ); + } + [Equal.symbol](that: unknown): boolean { + return that instanceof UploadKey && that.id === this.id; + } + [Hash.symbol](): number { + return Hash.string(this.id); + } +} + +/** + * Every upload of a composer attachment, shared by key: the composer holds the attachments it + * shows or has queued, and a send joins the upload already running instead of starting another. + * An upload is interrupted once nothing holds it. + */ export function createComposerAttachmentUploadQueue(options: { readonly upload: ( request: ComposerAttachmentUploadRequest, signal: AbortSignal, onProgress: (progress: number) => void, - ) => Promise; + ) => Promise; + /** Saves a finished upload's id; the upload is ready once this resolves. */ + readonly onUploaded?: ( + request: ComposerAttachmentUploadRequest, + uploadedId: string, + ) => Promise; readonly onChange: (states: Readonly>) => void; }) { - const jobs = new Map< - string, - { readonly controller: AbortController; readonly done: Promise } - >(); - let desired = new Map(); let states: Readonly> = {}; - let disposed = false; - function setState(key: string, state: ComposerAttachmentUploadState | undefined) { const previous = states[key]; if ( @@ -161,82 +193,139 @@ export function createComposerAttachmentUploadQueue(options: { options.onChange(states); } - function pump() { - if (disposed) return; - for (const [key, request] of desired) { - if (jobs.size >= 3) break; - if (jobs.has(key) || states[key]?.status === "ready" || states[key]?.status === "failed") - continue; + const permits = Semaphore.makeUnsafe(3); + // An interrupted transfer settles before its attachment can upload again. + const settling = new Map>(); + const running = new Set>(); + + const transfer = (key: UploadKey) => + Effect.suspend(() => { + const previous = settling.get(key.id); const controller = new AbortController(); - setState(key, { status: "uploading", progress: 0 }); - // Publish the job before starting async work, including synchronous test transports. - const done = Promise.resolve() - .then(() => - options.upload(request, controller.signal, (progress) => { - if (controller.signal.aborted) return; - setState(key, { - status: "uploading", - progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, - }); + let done: Promise | undefined; + const start = () => { + if (states[key.id]?.status !== "uploading") + setState(key.id, { status: "uploading", progress: 0 }); + done = options.upload(key.request!, controller.signal, (progress) => { + if (controller.signal.aborted) return; + setState(key.id, { + status: "uploading", + progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, + }); + }); + return done; + }; + return Effect.promise(() => previous ?? Promise.resolve()).pipe( + Effect.andThen(Effect.tryPromise({ try: start, catch: uploadError })), + Semaphore.withPermits(permits, 1), + Effect.tap((uploadedId) => + Effect.tryPromise({ + try: () => options.onUploaded?.(key.request!, uploadedId) ?? Promise.resolve(), + catch: uploadError, }), - ) - .then((persisted) => { - if (!controller.signal.aborted && desired.has(key)) { - if (!persisted) desired.delete(key); - setState(key, persisted ? { status: "ready" } : undefined); - } - }) - .catch((error: unknown) => { - if (!controller.signal.aborted && desired.has(key)) { - setState(key, { - status: "failed", - reason: error instanceof Error ? error.message : "Upload failed. Tap to retry.", + ), + Effect.tap(() => Effect.sync(() => setState(key.id, { status: "ready" }))), + Effect.tapError((error) => + Effect.sync(() => setState(key.id, { status: "failed", reason: error.message })), + ), + Effect.onInterrupt(() => + Effect.sync(() => { + controller.abort(); + const settled = (done ?? Promise.resolve("")).then( + () => undefined, + () => undefined, + ); + settling.set(key.id, settled); + void settled.then(() => { + if (settling.get(key.id) === settled) settling.delete(key.id); }); - } - }) - .finally(() => { - jobs.delete(key); - pump(); - }); - jobs.set(key, { controller, done }); + }), + ), + ); + }); + + const scope = Scope.makeUnsafe(); + const uploads = Effect.runSync( + Scope.provide(scope)( + RcMap.make({ + lookup: Effect.fn("ComposerAttachmentUploads.lookup")(function* (key: UploadKey) { + setState(key.id, { status: "uploading", progress: 0 }); + yield* Effect.addFinalizer(() => Effect.sync(() => setState(key.id, undefined))); + const fiber = yield* Effect.forkScoped(transfer(key)); + running.add(fiber); + fiber.addObserver(() => running.delete(fiber)); + return fiber; + }), + }), + ), + ); + + const holders = new Map(); + let requested: ReadonlyArray = []; + let disposed = false; + const release = (id: string) => { + const holder = holders.get(id); + if (!holder) return; + holders.delete(id); + Effect.runFork(Scope.close(holder, Exit.void)); + }; + + /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */ + function sync(requests: ReadonlyArray) { + if (disposed) return; + requested = requests; + const keys = new Map(requests.map((request) => [UploadKey.of(request).id, request])); + for (const id of holders.keys()) { + if (!keys.has(id)) release(id); + } + for (const [id, request] of keys) { + if (holders.has(id)) continue; + const holder = Scope.makeUnsafe(); + holders.set(id, holder); + Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, UploadKey.of(request)))); } } return { - sync(requests: ReadonlyArray) { - if (disposed) return; - desired = new Map( - requests.map((request) => [ - composerAttachmentUploadKey(request.environmentId, request.attachment.id), - request, - ]), + sync, + /** Uploads the attachment, joining its running upload when there is one. */ + upload(request: ComposerAttachmentUploadRequest, signal?: AbortSignal): Promise { + if (disposed) return Promise.reject(new Error("Upload cancelled.")); + return Effect.runPromise( + RcMap.get(uploads, UploadKey.of(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped), + signal ? { signal } : undefined, ); - for (const [key, job] of jobs) { - if (!desired.has(key)) job.controller.abort(); - } - for (const key of Object.keys(states)) { - if (!desired.has(key)) setState(key, undefined); - } - for (const key of desired.keys()) { - if (!states[key]) setState(key, { status: "uploading", progress: 0 }); - } - pump(); }, retry(environmentId: EnvironmentId, attachmentId: string) { - const key = composerAttachmentUploadKey(environmentId, attachmentId); - if (states[key]?.status !== "failed") return; - setState(key, undefined); - pump(); + const id = composerAttachmentUploadKey(environmentId, attachmentId); + if (states[id]?.status !== "failed") return; + Effect.runSync(RcMap.invalidate(uploads, new UploadKey(id, null))); + release(id); + sync(requested); }, async settled() { - while (jobs.size > 0) await Promise.all([...jobs.values()].map((job) => job.done)); + while (running.size > 0) { + await Effect.runPromise(Fiber.awaitAll([...running])); + } }, dispose() { disposed = true; - desired.clear(); - for (const job of jobs.values()) job.controller.abort(); + holders.clear(); + Effect.runFork(Scope.close(scope, Exit.void)); states = {}; options.onChange(states); }, }; } + +function uploadError(cause: unknown): ComposerAttachmentUploadError { + return new ComposerAttachmentUploadError({ + message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.", + cause, + }); +} + +class ComposerAttachmentUploadError extends Schema.TaggedError()( + "ComposerAttachmentUploadError", + { message: Schema.String, cause: Schema.Defect() }, +) {} diff --git a/apps/mobile/src/state/composer-attachment-uploads.test.ts b/apps/mobile/src/state/composer-attachment-uploads.test.ts index 81a542a5aca1c..ac8bed2a42a75 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.test.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.test.ts @@ -369,7 +369,8 @@ it("keeps an unexpected draft flush error blocking Send", async () => { expect(composerAttachmentUploadBlockReason(sendInput())).toBe( "Retry or remove the failed attachment", ); - expect(native.deleted).toEqual(["pending-1"]); + // The server sweeps pending uploads nothing claims; a retry uploads again. + expect(native.deleted).toEqual([]); }); it("uploads draft attachments for threads whose ids contain colons", () => { diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index 8e2921c5f62d2..3539c077d34e6 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -1,10 +1,9 @@ -import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { useAtomValue } from "@effect/atom-react"; import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; -import { Atom, AsyncResult } from "effect/reactivity"; +import { Atom } from "effect/reactivity"; -import { prepareTurnAttachments } from "../lib/attachmentUpload"; +import { uploadComposerAttachment } from "../lib/attachmentUpload"; import { retainComposerAttachmentFileForPreview } from "../lib/composerAttachmentPreviewRetention"; import { isFileBackedComposerAttachment, @@ -193,8 +192,68 @@ const composerAttachmentUploadRequestsAtom = Atom.make((get) => { }); }).pipe(Atom.withLabel("mobile:composer-attachment-upload-requests")); +/** Saves a finished upload's id on every draft and queued message holding the attachment. */ +async function recordComposerAttachmentUpload( + request: ComposerAttachmentUploadRequest, + uploadedId: string, +): Promise { + const { environmentId, attachment } = request; + const uploaded: DraftComposerAttachment = { + ...attachment, + uploadedAttachmentId: uploadedId, + uploadEnvironmentId: environmentId, + }; + const queued = flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ); + const knownEnvironmentIds = appAtomRegistry + .get(workspaceConnections.environmentsAtom) + .map((environment) => environment.environmentId); + let draftsChanged = false; + for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) { + if ( + composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft) === environmentId && + draft.attachments.some((candidate) => candidate.id === attachment.id) + ) { + draftsChanged = setComposerDraftAttachmentUpload(key, uploaded) || draftsChanged; + } + } + await recordQueuedAttachmentUpload(environmentId, uploaded); + if (!draftsChanged) return; + // A failed draft save only costs a re-upload after restart; the outbox saves its own references. + await flushComposerDrafts().catch((error: unknown) => { + if (!isComposerDraftPersistenceError(error)) throw error; + console.warn("[composer-attachments] could not save the uploaded reference", error); + }); +} + +function composerAttachmentUploads() { + uploadQueue ??= createComposerAttachmentUploadQueue({ + onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states), + upload: ({ environmentId, attachment }, signal, onProgress) => { + const release = isFileBackedComposerAttachment(attachment) + ? retainComposerAttachmentFileForPreview(attachment) + : undefined; + return uploadComposerAttachment(environmentId, attachment, signal, onProgress).finally(() => + release?.(), + ); + }, + onUploaded: recordComposerAttachmentUpload, + }); + return uploadQueue; +} + +/** Every send uploads through here, so it joins the composer's upload of the same attachment. */ +export function uploadComposerAttachmentShared( + environmentId: EnvironmentId, + attachment: DraftComposerAttachment, + signal?: AbortSignal, +): Promise { + return composerAttachmentUploads().upload({ environmentId, attachment }, signal); +} + /** - * Uploads draft attachments to their environment as soon as it is connected and + * Uploads draft and queued attachments to their environment as soon as it is connected and * operable. The queue follows the requests atom for its whole lifetime. */ export function createComposerAttachmentUploadWorker( @@ -203,54 +262,7 @@ export function createComposerAttachmentUploadWorker( > = composerAttachmentUploadRequestsAtom, ) { ensureComposerDraftsLoaded(); - const queue = createComposerAttachmentUploadQueue({ - onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states), - upload: async ({ environmentId, attachment }, signal, onProgress) => { - const release = isFileBackedComposerAttachment(attachment) - ? retainComposerAttachmentFileForPreview(attachment) - : undefined; - const prepared = await settlePromise(() => - prepareTurnAttachments({ - environmentId, - attachments: [attachment], - supportsImageUploads: true, - signal, - onUploadProgress: (_, progress) => onProgress(progress), - persistUploadedReferences: async ([uploaded]) => { - if (signal.aborted || !uploaded) return "abandon"; - const queued = flattenQueuedThreadMessages( - appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), - ); - const knownEnvironmentIds = appAtomRegistry - .get(workspaceConnections.environmentsAtom) - .map((environment) => environment.environmentId); - let retained = false; - for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) { - if ( - composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft) === - environmentId && - draft.attachments.some((candidate) => candidate.id === attachment.id) - ) { - retained = setComposerDraftAttachmentUpload(key, uploaded) || retained; - } - } - retained = (await recordQueuedAttachmentUpload(environmentId, uploaded)) || retained; - if (!retained) return "abandon"; - // Draft-save failure only costs re-upload after restart; the outbox saves references independently, so preserve accepted uploads. - await flushComposerDrafts().catch((error: unknown) => { - if (!isComposerDraftPersistenceError(error)) throw error; - console.warn("[composer-attachments] could not save the uploaded reference", error); - }); - return "persisted"; - }, - }), - ); - release?.(); - if (AsyncResult.isFailure(prepared)) throw squashAtomCommandFailure(prepared); - return prepared.value.status === "ready"; - }, - }); - uploadQueue = queue; + const queue = composerAttachmentUploads(); const unsubscribe = appAtomRegistry.subscribe(requestsAtom, (requests) => queue.sync(requests), { immediate: true, }); diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index 263c48195d75d..af67248c31c1a 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -107,6 +107,7 @@ vi.mock("./git", () => ({ gitEnvironment: { preparePullRequestThread: state.chec vi.mock("../lib/modelOptions", () => ({ isModelSelectionUnavailable: state.modelUnavailable })); vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => "abcd" })); vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: state.prepare })); +vi.mock("./composer-attachment-uploads", () => ({ uploadComposerAttachmentShared: vi.fn() })); vi.mock("./thread-outbox", async () => { const { createThreadOutboxManager } = await import("./thread-outbox-manager"); const { appAtomRegistry } = await import("./atom-registry"); diff --git a/apps/mobile/src/state/threadOutboxDelivery.test.ts b/apps/mobile/src/state/threadOutboxDelivery.test.ts index f7d67f4349397..8800757ab5d56 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.test.ts @@ -84,6 +84,7 @@ vi.mock("../lib/uuid", () => ({ vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: harness.prepareTurnAttachments, })); +vi.mock("./composer-attachment-uploads", () => ({ uploadComposerAttachmentShared: vi.fn() })); vi.mock("./projects", () => ({ environmentProjects: {} })); diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index 74cf7597e0ef7..79e0ceced038c 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -49,6 +49,7 @@ import { import { environmentProjects } from "./projects"; import { environmentServerConfigsAtom, serverEnvironment } from "./server"; import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; +import { uploadComposerAttachmentShared } from "./composer-attachment-uploads"; import { queuedAttachmentsStillUploading, uploadingComposerAttachmentKeys, @@ -233,6 +234,8 @@ export async function prepareQueuedMessageAttachments(queuedMessage: QueuedThrea environmentId: queuedMessage.environmentId, attachments: queuedMessage.attachments, supportsImageUploads: true, + uploadAttachment: (attachment) => + uploadComposerAttachmentShared(queuedMessage.environmentId, attachment), persistUploadedReferences: async (draftAttachments) => { if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { return "abandon"; diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index 200b2b165c6b5..bde1d00c5da99 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -1241,9 +1241,11 @@ describe("mobile composer drafts", () => { }); appendComposerDraftAttachments("environment-1:thread-1", [image]); await flushComposerDrafts(); + // Without image uploads the image is inlined, so nothing goes through the upload registry. const preparing = prepareTurnAttachments({ environmentId: EnvironmentId.make("environment-1"), attachments: [image], + uploadAttachment: vi.fn(), }); onTestFinished(async () => { read.resolve(); diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 08b9e20b27f60..d9eb8a3937c27 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -72,6 +72,7 @@ import { pendingThreadCreationMessage } from "./pending-thread-creation"; import { composerAttachmentUploadBlockReason, composerAttachmentUploadsAtom, + uploadComposerAttachmentShared, } from "./composer-attachment-uploads"; import { appendComposerDraftAttachments, @@ -514,6 +515,8 @@ export function useThreadComposerState(input: { environmentId: thread.environmentId, attachments: draft.attachments, supportsImageUploads: capabilities !== undefined, + uploadAttachment: (attachment) => + uploadComposerAttachmentShared(thread.environmentId, attachment), }), ); if (AsyncResult.isFailure(prepared)) { From 89a7f1d4c77a2fd40f2b458a70f35e358fdf31a6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 10:10:12 -0800 Subject: [PATCH 2/5] fix(mobile): a new thread's draft keeps the project picked for it The draft's project stamp is the new thread's selection. Picking a project creates or retargets the draft in the same handler, so the provider no longer creates a draft from whatever project an effect saw during render, which could stamp a fallback project on another environment while the screen showed the picked one. Co-Authored-By: Claude Opus 5.5 --- .../features/threads/NewTaskDraftScreen.tsx | 18 ++++----- .../threads/new-task-flow-provider.test.tsx | 27 +++++++++++-- .../threads/new-task-flow-provider.tsx | 40 ++++++++----------- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx index 201204a8a57bf..ef279152d2581 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx @@ -431,15 +431,16 @@ export function NewTaskDraftScreen(props: { if (appliedInitialProjectKeyRef.current === directProjectKey) { return; } + // setProject creates the draft, so it also runs when the fallback already shows this project. + const showsDirectProject = + flow.draftKey !== null && + selectedProject?.environmentId === directProject.environmentId && + selectedProject.id === directProject.id; if (props.initialProjectRef?.branch) { - if ( - selectedProject?.environmentId !== directProject.environmentId || - selectedProject.id !== directProject.id - ) { + if (!showsDirectProject || !flow.draftKey) { setProject(directProject); return; } - if (!flow.draftKey) return; // Checkout completed before mount; local mode reuses it while worktree mode would create another. updateComposerDraftSettings(flow.draftKey, { workspaceSelection: { @@ -451,12 +452,7 @@ export function NewTaskDraftScreen(props: { }); } appliedInitialProjectKeyRef.current = directProjectKey; - if ( - selectedProject?.environmentId === directProject.environmentId && - selectedProject.id === directProject.id - ) { - return; - } + if (showsDirectProject) return; setProject(directProject); return; } diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx index 05ccf29b2ae70..0972d937696e1 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx @@ -14,7 +14,7 @@ import * as Layer from "effect/Layer"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; -import { act, useLayoutEffect } from "react"; +import { act, useEffect, useLayoutEffect } from "react"; import { createRoot } from "react-dom/client"; import { expect, it, vi } from "vite-plus/test"; import type { ComposerDraft } from "../../state/use-composer-drafts"; @@ -213,11 +213,16 @@ function setup() { mocks.writeSettings.mockImplementation(write); mocks.writeText.mockImplementation((key, text) => write(key, { text })); let current: ReturnType; - function Probe() { + function Probe(props: { readonly pickOnMount?: EnvironmentProject }) { const value = useNewTaskFlow(); useLayoutEffect(() => { current = value; }); + // Mirrors the draft screen applying its route project on mount, before the provider's effects run. + useEffect(() => { + if (props.pickOnMount) value.setProject(props.pickOnMount); + // oxlint-disable-next-line react/exhaustive-effect-dependencies -- picks once on mount; setProject changes every render + }, []); return null; } const root = createRoot(document.createElement("div")); @@ -245,16 +250,20 @@ function setup() { get current() { return current; }, - async render(active: boolean) { + async render(active: boolean, pickOnMount?: EnvironmentProject) { await act(async () => root.render( - + , ), ); + // The draft screen applies a project whenever it shows without a draft. + if (active && current.draftKey === null) { + await act(async () => current.setProject(projects[0]!)); + } }, async emit(cwd: string, branch: string, isRepo = true) { await act(async () => emitters.get(cwd)!(branch, isRepo)); @@ -270,6 +279,16 @@ function setup() { }; } +it("keeps the draft on a project the screen picks as it mounts", async () => { + using screen = setup(); + await screen.render(true, screen.projects[2]); + expect(screen.current.selectedProject).toEqual(screen.projects[2]); + expect(screen.snapshot(screen.current.draftKey).project).toEqual({ + environmentId: "two", + projectId: "b", + }); +}); + it("owns a status stream only while the draft is active, retaining draft choices on reopen", async () => { using screen = setup(); await screen.render(false); diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index 5852206095a8f..83d87d6eba882 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -2,7 +2,7 @@ import { ActiveNewTaskContext } from "../../state/active-new-task"; import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits"; import { useNowMinuteMs } from "../../lib/clock"; import * as Dialog from "../../components/ui/Dialog"; -import React, { useEffect, useRef, useState } from "react"; +import React, { useRef, useState } from "react"; import type { EnvironmentId, @@ -304,11 +304,23 @@ export function NewTaskFlowProvider( // binding outlives the draft's project stamp (submit clears it). const updateFlow = (patch: Partial) => updateNewTaskFlowState(flowKey, { ...readFlow(), ...patch }); + // A draft's project stamp is the selection: picking a project writes it, and uploads and + // the send read it, so the screen cannot show one project while the draft holds another. + const draftStamp = useComposerDraft(activeDraftKey).project; + const stampedProject = + draftStamp === undefined + ? undefined + : projects.find( + (project) => + project.environmentId === draftStamp.environmentId && + project.id === draftStamp.projectId, + ); const selectedEnvironmentId = - selectedEnvironmentIdOverride !== null && + stampedProject?.environmentId ?? + (selectedEnvironmentIdOverride !== null && projects.some((project) => project.environmentId === selectedEnvironmentIdOverride) ? selectedEnvironmentIdOverride - : (projects[0]?.environmentId ?? null); + : (projects[0]?.environmentId ?? null)); const projectsForEnvironment = pipe( projects, Arr.filter((project) => project.environmentId === selectedEnvironmentId), @@ -319,7 +331,8 @@ export function NewTaskFlowProvider( const selectedProject = routeDraftResolution.kind === "pending" ? null - : (projectsForEnvironment.find( + : (stampedProject ?? + projectsForEnvironment.find( (project) => scopedProjectKey(project.environmentId, project.id) === selectedProjectKey, ) ?? projectsForEnvironment[0] ?? @@ -333,25 +346,6 @@ export function NewTaskFlowProvider( const serverConfigs = useServerConfigs(); const { connectedEnvironments } = useRemoteConnectionStatus(); const selectedProjectDraftKey = selectedProject ? activeDraftKey : null; - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- creates the persistent draft in the new-task flow store once a project is selected, before the composer mounts - useEffect(() => { - // A route that names a draft binds that draft, or returns to the picker. - if ( - props.active === false || - props.routeDraftId !== undefined || - activeDraftKey !== null || - selectedProject === null - ) { - return; - } - // Creates the persistent draft once the project selection is ready; the key must exist before the composer mounts. - updateNewTaskFlowState(flowKey, { - draftKey: createNewTaskDraft({ - environmentId: selectedProject.environmentId, - projectId: selectedProject.id, - }), - }); - }, [activeDraftKey, flowKey, selectedProject, props.active, props.routeDraftId]); const activePendingKey = props.active === false ? null From 3c132557f36632f7c3b77db96a37dc201a03e232 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 10:41:01 -0800 Subject: [PATCH 3/5] refactor(mobile): record queued uploads with a conflict-free outbox modify The outbox manager gains modify(messageId, change), which applies the change to the message as it stands when its serialized turn comes. Recording an upload on a queued message uses it, so two attachments finishing together compose instead of the later write losing a revision race, and the reread-and-retry loop is gone. Co-Authored-By: Claude Opus 5.5 --- .../src/state/composer-attachment-uploads.ts | 46 +++++++++---------- .../mobile/src/state/thread-outbox-manager.ts | 41 +++++++++++++++++ apps/mobile/src/state/thread-outbox.test.ts | 36 +++++++++++++++ apps/mobile/src/state/thread-outbox.ts | 8 ++++ 4 files changed, 106 insertions(+), 25 deletions(-) diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index 3539c077d34e6..a4ecf51dbccd8 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -21,8 +21,7 @@ import { environmentServerConfigsAtom } from "./server"; import { flattenQueuedThreadMessages, threadOutboxManager, - threadOutboxRevision, - updateThreadOutboxMessage, + modifyThreadOutboxMessage, type QueuedThreadMessage, } from "./thread-outbox"; import { editingQueuedMessageIdsAtom } from "./use-thread-outbox"; @@ -138,29 +137,26 @@ function withUploadedAttachment( async function recordQueuedAttachmentUpload( environmentId: EnvironmentId, uploaded: DraftComposerAttachment, -): Promise { - const awaiting = () => { - const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); - return flattenQueuedThreadMessages( - appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), - ).flatMap((message) => { - if (message.environmentId !== environmentId || editing[message.messageId]) return []; - const updated = withUploadedAttachment(message, uploaded); - return updated === null - ? [] - : [{ updated, revision: threadOutboxRevision(message.messageId) }]; - }); - }; - let recorded = false; - // A sibling attachment finishing at the same time bumps the revision; reread and retry. - for (let attempt = 0; attempt < 3; attempt++) { - const pending = awaiting(); - if (pending.length === 0) break; - for (const { updated, revision } of pending) { - recorded = (await updateThreadOutboxMessage(updated, revision)) || recorded; - } - } - return recorded; +): Promise { + const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); + const holders = flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ).filter( + (message) => + message.environmentId === environmentId && + !editing[message.messageId] && + withUploadedAttachment(message, uploaded) !== null, + ); + await Promise.all( + holders.map((message) => + // An editor that opened meanwhile owns the message; its draft records the upload. + modifyThreadOutboxMessage(message.messageId, (current) => + appAtomRegistry.get(editingQueuedMessageIdsAtom)[current.messageId] + ? null + : withUploadedAttachment(current, uploaded), + ), + ), + ); } // Each connected environment's session is read here so a granted or revoked diff --git a/apps/mobile/src/state/thread-outbox-manager.ts b/apps/mobile/src/state/thread-outbox-manager.ts index 732db0159b7f7..a83537fdb0a0c 100644 --- a/apps/mobile/src/state/thread-outbox-manager.ts +++ b/apps/mobile/src/state/thread-outbox-manager.ts @@ -176,6 +176,46 @@ export function createThreadOutboxManager(options: ThreadOutboxManagerOptions) { return true; }); + /** + * Applies `change` to the message as it stands when this mutation's turn comes, so concurrent + * changes compose instead of failing on each other's revision. `null` leaves it unchanged. + */ + const modify = ( + messageId: MessageId, + change: (message: QueuedThreadMessage) => QueuedThreadMessage | null, + ): Promise => + serialize(async () => { + const current = currentMessages().find((candidate) => candidate.messageId === messageId); + const next = current === undefined ? null : change(current); + if (current === undefined || next === null) return false; + const revision = revisions.get(messageId) ?? 0; + try { + await options.storage.write(next); + } catch (cause) { + throw new ThreadOutboxManagerError({ + operation: "update", + environmentId: next.environmentId, + threadId: next.threadId, + messageId, + cause, + }); + } + // An enqueue publishes without waiting its turn; when one replaced the message during the + // write, it wins and stays durable. + if ((revisions.get(messageId) ?? 0) !== revision || !currentMessages().includes(current)) { + const winner = currentMessages().find((candidate) => candidate.messageId === messageId); + if (winner !== undefined) { + try { + await options.storage.write(winner); + } catch {} + } + return false; + } + bumpRevision(messageId); + setMessages(currentMessages().map((candidate) => (candidate === current ? next : candidate))); + return true; + }); + // Check live ownership too; editor locks can change without message revision updates. const remove = ( message: QueuedThreadMessage, @@ -347,6 +387,7 @@ export function createThreadOutboxManager(options: ThreadOutboxManagerOptions) { confirmQueued, revisionOf: (messageId: MessageId): number => revisions.get(messageId) ?? 0, update, + modify, remove, clearEnvironment, }; diff --git a/apps/mobile/src/state/thread-outbox.test.ts b/apps/mobile/src/state/thread-outbox.test.ts index 95333fc5925d2..5ff7bd53a1c7d 100644 --- a/apps/mobile/src/state/thread-outbox.test.ts +++ b/apps/mobile/src/state/thread-outbox.test.ts @@ -718,6 +718,42 @@ describe("thread outbox", () => { registry.dispose(); }); + it("composes concurrent modifications of one message instead of rejecting the later one", async () => { + const registry = AtomRegistry.make(); + const stored = new Map(); + const manager = createThreadOutboxManager({ + registry, + storage: { + load: async () => ({ messages: [], errors: [] }), + write: async (message) => { + stored.set(message.messageId, message); + }, + remove: async () => undefined, + }, + }); + const message = queuedMessage({ + messageId: "message-two-uploads", + createdAt: "2026-06-08T10:00:01.000Z", + }); + await manager.enqueue(message); + + // Two attachments finishing together each record their upload on the same message. + const results = await Promise.all([ + manager.modify(message.messageId, (current) => ({ ...current, text: `${current.text} a` })), + manager.modify(message.messageId, (current) => ({ ...current, text: `${current.text} b` })), + ]); + + expect(results).toEqual([true, true]); + const composed = { ...message, text: `${message.text} a b` }; + expect(registry.get(manager.queuedMessagesByThreadKeyAtom)).toEqual({ + "environment-1:thread-1": [composed], + }); + expect(stored.get(message.messageId)).toEqual(composed); + await manager.remove(composed); + await expect(manager.modify(message.messageId, (current) => current)).resolves.toBe(false); + registry.dispose(); + }); + it("rejects a stale revision before its payload reaches durable storage", async () => { const registry = AtomRegistry.make(); const writes: string[] = []; diff --git a/apps/mobile/src/state/thread-outbox.ts b/apps/mobile/src/state/thread-outbox.ts index 4a9c7d666e129..24f3c1512abce 100644 --- a/apps/mobile/src/state/thread-outbox.ts +++ b/apps/mobile/src/state/thread-outbox.ts @@ -32,6 +32,14 @@ export function updateThreadOutboxMessage( return threadOutboxManager.update(message, expectedRevision); } +/** Changes the message as it is when its turn comes; false when it is gone or `change` returns null. */ +export function modifyThreadOutboxMessage( + messageId: QueuedThreadMessage["messageId"], + change: (message: QueuedThreadMessage) => QueuedThreadMessage | null, +): Promise { + return threadOutboxManager.modify(messageId, change); +} + export function threadOutboxRevision(messageId: QueuedThreadMessage["messageId"]): number { return threadOutboxManager.revisionOf(messageId); } From 893a59959fee41ddce4ea97cd6ab19fc496cbc98 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 10:54:00 -0800 Subject: [PATCH 4/5] refactor(mobile): prepare turn attachments as an Effect joining shared uploads The upload registry's upload returns an Effect that joins the upload fiber, and prepareTurnAttachments is an Effect.fn over it, so a send waits on the shared upload without a promise round trip and stops waiting when interrupted. Upload failures carry one tagged error whose cause still decides whether delivery retries. Co-Authored-By: Claude Opus 5.5 --- apps/mobile/src/lib/attachmentUpload.test.ts | 996 ++++++++++-------- apps/mobile/src/lib/attachmentUpload.ts | 93 +- .../lib/composerAttachmentUploadQueue.test.ts | 67 +- .../src/lib/composerAttachmentUploadQueue.ts | 19 +- .../src/state/composer-attachment-uploads.ts | 5 +- apps/mobile/src/state/thread-outbox-model.ts | 3 + .../threadOutboxDelivery.permissions.test.ts | 39 +- .../src/state/threadOutboxDelivery.test.ts | 45 +- apps/mobile/src/state/threadOutboxDelivery.ts | 42 +- .../src/state/use-composer-drafts.test.ts | 116 +- .../src/state/use-thread-composer-state.ts | 17 +- 11 files changed, 768 insertions(+), 674 deletions(-) diff --git a/apps/mobile/src/lib/attachmentUpload.test.ts b/apps/mobile/src/lib/attachmentUpload.test.ts index cba8f1566717a..fb7fa933fbca9 100644 --- a/apps/mobile/src/lib/attachmentUpload.test.ts +++ b/apps/mobile/src/lib/attachmentUpload.test.ts @@ -1,6 +1,7 @@ import { EnvironmentId, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts"; import * as Option from "effect/Option"; -import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; +import { describe, expect, it } from "@effect/vitest"; +import { beforeEach, vi } from "vite-plus/test"; const mocks = vi.hoisted(() => ({ canOperate: true, @@ -89,6 +90,10 @@ vi.mock("expo-file-system", () => ({ UploadType: { BINARY_CONTENT: 0 }, })); +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import { uploadError } from "./composerAttachmentUploadQueue"; import { prepareTurnAttachments as prepareWithUploads, releasePendingAttachmentUploads, @@ -97,23 +102,37 @@ import { } from "./attachmentUpload"; /** Uploads each attachment directly, as the shared registry does for one holder. */ -function prepareTurnAttachments( +function prepare( input: Omit[0], "uploadAttachment"> & { readonly onUploadProgress?: (attachmentId: string, progress: number) => void; }, ) { - const signal = input.signal ?? new AbortController().signal; return prepareWithUploads({ ...input, + // An interrupted send waits for its transfer to settle, as the shared registry does. uploadAttachment: (attachment) => - uploadComposerAttachment( - input.environmentId, - attachment, - signal, - input.onUploadProgress - ? (progress) => input.onUploadProgress?.(attachment.id, progress) - : undefined, - ), + Effect.suspend(() => { + const controller = new AbortController(); + const upload = uploadComposerAttachment( + input.environmentId, + attachment, + controller.signal, + input.onUploadProgress + ? (progress) => input.onUploadProgress?.(attachment.id, progress) + : undefined, + ); + return Effect.tryPromise({ try: () => upload, catch: uploadError }).pipe( + Effect.onInterrupt(() => + Effect.promise(() => { + controller.abort(); + return upload.then( + () => undefined, + () => undefined, + ); + }), + ), + ); + }), }); } import type { DraftComposerAttachment } from "./composerImages"; @@ -198,68 +217,80 @@ describe("prepareTurnAttachments", () => { mocks.upload.mockResolvedValue({ status: 204, body: "", headers: {} }); }); - it("does not mint or transfer attachments without task operation access", async () => { - mocks.canOperate = false; - await expect( - prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - supportsImageUploads: true, - }), - ).rejects.toThrow("cannot upload attachments"); - expect(mocks.runAtomCommand).not.toHaveBeenCalled(); - expect(mocks.upload).not.toHaveBeenCalled(); - }); - - it("rechecks access after a signed URL is minted before sending any bytes", async () => { - mocks.runAtomCommand.mockImplementationOnce(async () => { + it.effect("does not mint or transfer attachments without task operation access", () => + Effect.gen(function* () { mocks.canOperate = false; - return { - _tag: "Success", - value: { - attachmentId: MINTED_ID, - relativeUrl: "/api/attachments/upload/signed", - expiresAt: 1, - }, - }; - }); - const warning = vi.spyOn(console, "warn").mockImplementation(() => {}); - try { - await expect( - prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - supportsImageUploads: true, - }), - ).rejects.toThrow("cannot upload attachments"); + expect( + (yield* Effect.flip( + prepare({ + environmentId, + attachments: [fileBackedImage], + supportsImageUploads: true, + }), + )).message, + ).toContain("cannot upload attachments"); + expect(mocks.runAtomCommand).not.toHaveBeenCalled(); expect(mocks.upload).not.toHaveBeenCalled(); - expect(mocks.runAtomCommand).toHaveBeenCalledTimes(1); - } finally { - warning.mockRestore(); - } - }); + }), + ); - it("does not replace an expired upload when access was revoked during verification", async () => { - mocks.executeAtomQuery.mockImplementationOnce(async () => { - mocks.canOperate = false; - return { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } }; - }); - await expect( - prepareTurnAttachments({ - environmentId, - attachments: [ - { - ...fileBackedImage, - uploadedAttachmentId: MINTED_ID, - uploadEnvironmentId: environmentId, + it.effect("rechecks access after a signed URL is minted before sending any bytes", () => + Effect.gen(function* () { + mocks.runAtomCommand.mockImplementationOnce(async () => { + mocks.canOperate = false; + return { + _tag: "Success", + value: { + attachmentId: MINTED_ID, + relativeUrl: "/api/attachments/upload/signed", + expiresAt: 1, }, - ], - supportsImageUploads: true, - }), - ).rejects.toThrow("cannot upload attachments"); - expect(mocks.runAtomCommand).not.toHaveBeenCalled(); - expect(mocks.upload).not.toHaveBeenCalled(); - }); + }; + }); + const warning = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + expect( + (yield* Effect.flip( + prepare({ + environmentId, + attachments: [fileBackedImage], + supportsImageUploads: true, + }), + )).message, + ).toContain("cannot upload attachments"); + expect(mocks.upload).not.toHaveBeenCalled(); + expect(mocks.runAtomCommand).toHaveBeenCalledTimes(1); + } finally { + warning.mockRestore(); + } + }), + ); + + it.effect("does not replace an expired upload when access was revoked during verification", () => + Effect.gen(function* () { + mocks.executeAtomQuery.mockImplementationOnce(async () => { + mocks.canOperate = false; + return { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } }; + }); + expect( + (yield* Effect.flip( + prepare({ + environmentId, + attachments: [ + { + ...fileBackedImage, + uploadedAttachmentId: MINTED_ID, + uploadEnvironmentId: environmentId, + }, + ], + supportsImageUploads: true, + }), + )).message, + ).toContain("cannot upload attachments"); + expect(mocks.runAtomCommand).not.toHaveBeenCalled(); + expect(mocks.upload).not.toHaveBeenCalled(); + }), + ); it("rechecks deletion permission before retrying a failed cleanup", async () => { mocks.runAtomCommand.mockImplementationOnce(async () => { @@ -272,256 +303,272 @@ describe("prepareTurnAttachments", () => { expect(mocks.runAtomCommand).toHaveBeenCalledTimes(1); }); - it("inlines a file-backed image lazily when the server lacks image uploads", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - }); + it.effect("inlines a file-backed image lazily when the server lacks image uploads", () => + Effect.gen(function* () { + const prepared = yield* prepare({ + environmentId, + attachments: [fileBackedImage], + }); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "image", - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - dataUrl: "data:image/png;base64,YWJj", - }, - ]); - expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith(fileBackedImage.fileUri); - expect(mocks.upload).not.toHaveBeenCalled(); - }); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,YWJj", + }, + ]); + expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith(fileBackedImage.fileUri); + expect(mocks.upload).not.toHaveBeenCalled(); + }), + ); // regression: #9713 read file-backed image drafts before enabling them - it("reads restored images from the current iOS document container for legacy sends", async () => { - const fileName = "33333333-3333-4333-8333-333333333333-photo.png"; - mocks.documentUri = - "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; - const oldUri = `file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [{ ...fileBackedImage, fileUri: oldUri, previewUri: oldUri }], - }); - expect(prepared.status).toBe("ready"); - expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith( - `${mocks.documentUri}/t3-composer-attachments/${fileName}`, - ); - }); + it.effect("reads restored images from the current iOS document container for legacy sends", () => + Effect.gen(function* () { + const fileName = "33333333-3333-4333-8333-333333333333-photo.png"; + mocks.documentUri = + "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; + const oldUri = `file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`; + const prepared = yield* prepare({ + environmentId, + attachments: [{ ...fileBackedImage, fileUri: oldUri, previewUri: oldUri }], + }); + expect(prepared.status).toBe("ready"); + expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith( + `${mocks.documentUri}/t3-composer-attachments/${fileName}`, + ); + }), + ); // regression: #9713 read file-backed image drafts before enabling them - it("rejects a legacy image without inline bytes or a file", async () => { - const { fileUri: _, ...missingImage } = fileBackedImage; - await expect( - prepareTurnAttachments({ environmentId, attachments: [missingImage] }), - ).rejects.toThrow("'photo.png' is no longer available. Attach the image again."); - }); + it.effect("rejects a legacy image without inline bytes or a file", () => + Effect.gen(function* () { + const { fileUri: _, ...missingImage } = fileBackedImage; + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [missingImage] }))).message, + ).toContain("'photo.png' is no longer available. Attach the image again."); + }), + ); - it.each(["resolve", "reject"] as const)( + it.effect.each(["resolve", "reject"] as const)( "abandons a canceled legacy image read that later %ss", - async (outcome) => { - const readStarted = Promise.withResolvers(); - const read = Promise.withResolvers(); - const controller = new AbortController(); - mocks.readBase64.mockImplementation(() => { - readStarted.resolve(); - return read.promise; - }); - const preparing = prepareTurnAttachments({ + (outcome) => + Effect.gen(function* () { + const readStarted = Promise.withResolvers(); + const read = Promise.withResolvers(); + mocks.readBase64.mockImplementation(() => { + readStarted.resolve(); + return read.promise; + }); + const preparing = yield* Effect.forkChild( + prepare({ environmentId, attachments: [fileBackedImage] }), + ); + yield* Effect.promise(() => readStarted.promise); + yield* Fiber.interrupt(preparing); + if (outcome === "resolve") read.resolve("YWJj"); + else read.reject(new Error("Native read failed after cancellation")); + + expect(Exit.hasInterrupts(yield* Fiber.await(preparing))).toBe(true); + }), + ); + + // regression: #9713 read file-backed image drafts before enabling them + it.effect("uploads a file-backed image from its owned copy without staging base64", () => + Effect.gen(function* () { + mocks.upload.mockImplementation( + async ( + _uri: string, + _url: string, + options: { onProgress?: (event: { bytesSent: unknown; totalBytes: unknown }) => void }, + ) => { + // expo-modules-jsi delivers native Int64 progress counts as BigInt. + options.onProgress?.({ bytesSent: BigInt(1), totalBytes: BigInt(4) }); + return { status: 204, body: "", headers: {} }; + }, + ); + const progress: Array = []; + const prepared = yield* prepare({ environmentId, attachments: [fileBackedImage], - signal: controller.signal, + supportsImageUploads: true, + onUploadProgress: (_, value) => progress.push(value), }); - await readStarted.promise; - controller.abort(); - if (outcome === "resolve") read.resolve("YWJj"); - else read.reject(new Error("Native read failed after cancellation")); - await expect(preparing).resolves.toEqual({ status: "abandoned" }); - }, + expect(progress).toEqual([0.25]); + + expect(mocks.upload).toHaveBeenCalledWith( + fileBackedImage.fileUri, + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ headers: { "Content-Type": "image/png" } }), + ); + expect(mocks.readBase64).not.toHaveBeenCalled(); + expect(mocks.writeFile).not.toHaveBeenCalled(); + expect(mocks.deleteFile).not.toHaveBeenCalled(); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + id: MINTED_ID, + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + }, + ]); + }), ); - // regression: #9713 read file-backed image drafts before enabling them - it("uploads a file-backed image from its owned copy without staging base64", async () => { - mocks.upload.mockImplementation( - async ( - _uri: string, - _url: string, - options: { onProgress?: (event: { bytesSent: unknown; totalBytes: unknown }) => void }, - ) => { - // expo-modules-jsi delivers native Int64 progress counts as BigInt. - options.onProgress?.({ bytesSent: BigInt(1), totalBytes: BigInt(4) }); - return { status: 204, body: "", headers: {} }; - }, - ); - const progress: Array = []; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - supportsImageUploads: true, - onUploadProgress: (_, value) => progress.push(value), - }); - - expect(progress).toEqual([0.25]); + it.effect("sends a document-picked picture with the mime it was uploaded under", () => + Effect.gen(function* () { + const prepared = yield* prepare({ + environmentId, + attachments: [documentPickedImage], + }); - expect(mocks.upload).toHaveBeenCalledWith( - fileBackedImage.fileUri, - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ headers: { "Content-Type": "image/png" } }), - ); - expect(mocks.readBase64).not.toHaveBeenCalled(); - expect(mocks.writeFile).not.toHaveBeenCalled(); - expect(mocks.deleteFile).not.toHaveBeenCalled(); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { + expect(mocks.upload).toHaveBeenCalledWith( + "file:///documents/photo.png", + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ headers: { "Content-Type": "image/png" } }), + ); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments[0]).toEqual({ type: "image", id: MINTED_ID, name: "photo.png", mimeType: "image/png", sizeBytes: 3, - }, - ]); - }); - - it("sends a document-picked picture with the mime it was uploaded under", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [documentPickedImage], - }); + }); + }), + ); - expect(mocks.upload).toHaveBeenCalledWith( - "file:///documents/photo.png", - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ headers: { "Content-Type": "image/png" } }), - ); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments[0]).toEqual({ - type: "image", - id: MINTED_ID, - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - }); - }); + it.effect("uploads an oversized document-picked image as a file", () => + Effect.gen(function* () { + const largeImage = { + ...documentPickedImage, + mimeType: "image/png", + sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1, + }; + const prepared = yield* prepare({ environmentId, attachments: [largeImage] }); - it("uploads an oversized document-picked image as a file", async () => { - const largeImage = { - ...documentPickedImage, - mimeType: "image/png", - sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1, - }; - const prepared = await prepareTurnAttachments({ environmentId, attachments: [largeImage] }); - - expect(mocks.runAtomCommand).toHaveBeenCalledWith( - expect.anything(), - mocks.createUploadUrl, - expect.objectContaining({ - input: { + expect(mocks.runAtomCommand).toHaveBeenCalledWith( + expect.anything(), + mocks.createUploadUrl, + expect.objectContaining({ + input: { + type: "file", + name: largeImage.name, + mimeType: "image/png", + sizeBytes: largeImage.sizeBytes, + }, + }), + expect.anything(), + ); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { type: "file", + id: MINTED_ID, name: largeImage.name, mimeType: "image/png", sizeBytes: largeImage.sizeBytes, }, - }), - expect.anything(), - ); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "file", - id: MINTED_ID, - name: largeImage.name, - mimeType: "image/png", - sizeBytes: largeImage.sizeBytes, - }, - ]); - expect(mocks.readBase64).not.toHaveBeenCalled(); - }); + ]); + expect(mocks.readBase64).not.toHaveBeenCalled(); + }), + ); - it("uploads generic file bytes directly and keeps mixed attachment order", async () => { - const pastedFile = { - ...file, - source: { _tag: "pasted-text" as const }, - }; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [pastedFile, image], - }); + it.effect("uploads generic file bytes directly and keeps mixed attachment order", () => + Effect.gen(function* () { + const pastedFile = { + ...file, + source: { _tag: "pasted-text" as const }, + }; + const prepared = yield* prepare({ + environmentId, + attachments: [pastedFile, image], + }); - expect(mocks.upload).toHaveBeenCalledWith( - "file:///documents/report.pdf", - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ - httpMethod: "POST", - uploadType: 0, - headers: { "Content-Type": "application/pdf" }, - }), - ); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments[0]).toEqual({ - type: "file", - id: MINTED_ID, - name: "report.pdf", - mimeType: "application/pdf", - sizeBytes: 42, - source: { _tag: "pasted-text" }, - }); - expect(prepared.attachments[1]?.type).toBe("image"); - expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); - expect(prepared.draftAttachments[0]).toEqual({ - ...pastedFile, - uploadedAttachmentId: MINTED_ID, - uploadEnvironmentId: environmentId, - }); - }); + expect(mocks.upload).toHaveBeenCalledWith( + "file:///documents/report.pdf", + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ + httpMethod: "POST", + uploadType: 0, + headers: { "Content-Type": "application/pdf" }, + }), + ); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments[0]).toEqual({ + type: "file", + id: MINTED_ID, + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 42, + source: { _tag: "pasted-text" }, + }); + expect(prepared.attachments[1]?.type).toBe("image"); + expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); + expect(prepared.draftAttachments[0]).toEqual({ + ...pastedFile, + uploadedAttachmentId: MINTED_ID, + uploadEnvironmentId: environmentId, + }); + }), + ); - it("uses the current connection when an environment reconnects during URL creation", async () => { - mocks.readAtom - .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://old-environment.example/" })) - .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://new-environment.example/" })); + it.effect("uses the current connection when an environment reconnects during URL creation", () => + Effect.gen(function* () { + mocks.readAtom + .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://old-environment.example/" })) + .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://new-environment.example/" })); - await prepareTurnAttachments({ environmentId, attachments: [file] }); + yield* prepare({ environmentId, attachments: [file] }); - expect(mocks.upload).toHaveBeenCalledWith( - file.fileUri, - "https://new-environment.example/api/attachments/upload/signed", - expect.anything(), - ); - }); + expect(mocks.upload).toHaveBeenCalledWith( + file.fileUri, + "https://new-environment.example/api/attachments/upload/signed", + expect.anything(), + ); + }), + ); - it("uploads a restored draft file from the current iOS document container", async () => { - const fileName = "33333333-3333-4333-8333-333333333333-report%20%23.pdf"; - const restoredFile = { - ...file, - fileUri: `file:///private/var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`, - }; - mocks.documentUri = - "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; - const currentUri = `${mocks.documentUri}/t3-composer-attachments/${fileName}`; - mocks.upload.mockImplementation(async (uri: string) => { - if (uri !== currentUri) { - throw new Error("File does not exist in the previous application container."); - } - return { status: 204, body: "", headers: {} }; - }); + it.effect("uploads a restored draft file from the current iOS document container", () => + Effect.gen(function* () { + const fileName = "33333333-3333-4333-8333-333333333333-report%20%23.pdf"; + const restoredFile = { + ...file, + fileUri: `file:///private/var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`, + }; + mocks.documentUri = + "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; + const currentUri = `${mocks.documentUri}/t3-composer-attachments/${fileName}`; + mocks.upload.mockImplementation(async (uri: string) => { + if (uri !== currentUri) { + throw new Error("File does not exist in the previous application container."); + } + return { status: 204, body: "", headers: {} }; + }); - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [restoredFile], - }); + const prepared = yield* prepare({ + environmentId, + attachments: [restoredFile], + }); - expect(prepared.status).toBe("ready"); - expect(mocks.upload).toHaveBeenCalledWith( - currentUri, - "https://environment.example/api/attachments/upload/signed", - expect.anything(), - ); - }); + expect(prepared.status).toBe("ready"); + expect(mocks.upload).toHaveBeenCalledWith( + currentUri, + "https://environment.example/api/attachments/upload/signed", + expect.anything(), + ); + }), + ); it("adds uploaded file references to durable drafts without changing images", () => { expect( @@ -555,204 +602,219 @@ describe("prepareTurnAttachments", () => { ]); }); - it("reuses a pending file upload from a previous outbox attempt", async () => { - const previouslyUploaded = { - ...file, - uploadedAttachmentId: "pending-existing-pdf", - uploadEnvironmentId: environmentId, - }; + it.effect("reuses a pending file upload from a previous outbox attempt", () => + Effect.gen(function* () { + const previouslyUploaded = { + ...file, + uploadedAttachmentId: "pending-existing-pdf", + uploadEnvironmentId: environmentId, + }; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [previouslyUploaded, image], - }); + const prepared = yield* prepare({ + environmentId, + attachments: [previouslyUploaded, image], + }); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "file", - id: "pending-existing-pdf", - name: "report.pdf", - mimeType: "application/pdf", - sizeBytes: 42, - }, - { - type: "image", - name: "screenshot.png", - mimeType: "image/png", - sizeBytes: 3, - dataUrl: "data:image/png;base64,YWJj", - }, - ]); - expect(prepared.pendingAttachmentIds).toEqual(["pending-existing-pdf"]); - expect(mocks.upload).not.toHaveBeenCalled(); - expect(mocks.runAtomCommand).not.toHaveBeenCalled(); - }); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "file", + id: "pending-existing-pdf", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 42, + }, + { + type: "image", + name: "screenshot.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,YWJj", + }, + ]); + expect(prepared.pendingAttachmentIds).toEqual(["pending-existing-pdf"]); + expect(mocks.upload).not.toHaveBeenCalled(); + expect(mocks.runAtomCommand).not.toHaveBeenCalled(); + }), + ); - it("uploads a file again when its saved pending upload has expired", async () => { - mocks.executeAtomQuery.mockResolvedValueOnce({ - _tag: "Failure", - error: { _tag: "AssetAttachmentNotFoundError" }, - }); - const previouslyUploaded = { - ...file, - uploadedAttachmentId: "pending-expired-pdf", - uploadEnvironmentId: environmentId, - }; - - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [previouslyUploaded], - }); + it.effect("uploads a file again when its saved pending upload has expired", () => + Effect.gen(function* () { + mocks.executeAtomQuery.mockResolvedValueOnce({ + _tag: "Failure", + error: { _tag: "AssetAttachmentNotFoundError" }, + }); + const previouslyUploaded = { + ...file, + uploadedAttachmentId: "pending-expired-pdf", + uploadEnvironmentId: environmentId, + }; - expect(mocks.upload).toHaveBeenCalledOnce(); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); - }); + const prepared = yield* prepare({ + environmentId, + attachments: [previouslyUploaded], + }); - it("uploads image bytes over HTTP while retaining the durable offline image", async () => { - const persisted = vi.fn(async () => "persisted" as const); - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [image], - supportsImageUploads: true, - persistUploadedReferences: persisted, - }); - expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { - encoding: "base64", - }); - expect(mocks.upload).toHaveBeenCalledWith( - "file:///cache/t3-upload-uuid", - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ headers: { "Content-Type": "image/png" } }), - ); - expect(mocks.deleteFile).toHaveBeenCalledExactlyOnceWith("file:///cache/t3-upload-uuid"); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { + expect(mocks.upload).toHaveBeenCalledOnce(); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); + }), + ); + + it.effect("uploads image bytes over HTTP while retaining the durable offline image", () => + Effect.gen(function* () { + const persisted = vi.fn(() => Effect.succeed("persisted" as const)); + const prepared = yield* prepare({ + environmentId, + attachments: [image], + supportsImageUploads: true, + persistUploadedReferences: persisted, + }); + expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { + encoding: "base64", + }); + expect(mocks.upload).toHaveBeenCalledWith( + "file:///cache/t3-upload-uuid", + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ headers: { "Content-Type": "image/png" } }), + ); + expect(mocks.deleteFile).toHaveBeenCalledExactlyOnceWith("file:///cache/t3-upload-uuid"); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + id: MINTED_ID, + name: image.name, + mimeType: image.mimeType, + sizeBytes: image.sizeBytes, + }, + ]); + expect(prepared.draftAttachments).toEqual([ + { ...image, uploadedAttachmentId: MINTED_ID, uploadEnvironmentId: environmentId }, + ]); + expect(persisted).toHaveBeenCalledWith(prepared.draftAttachments); + }), + ); + + it.effect("reuses an uploaded image and reuploads its local bytes after server expiry", () => + Effect.gen(function* () { + const saved = { + ...image, + uploadedAttachmentId: "saved-image", + uploadEnvironmentId: environmentId, + }; + const reused = yield* prepare({ + environmentId, + attachments: [saved], + supportsImageUploads: true, + }); + expect(reused.status === "ready" && reused.attachments[0]).toEqual({ type: "image", - id: MINTED_ID, + id: "saved-image", name: image.name, mimeType: image.mimeType, sizeBytes: image.sizeBytes, - }, - ]); - expect(prepared.draftAttachments).toEqual([ - { ...image, uploadedAttachmentId: MINTED_ID, uploadEnvironmentId: environmentId }, - ]); - expect(persisted).toHaveBeenCalledWith(prepared.draftAttachments); - }); - - it("reuses an uploaded image and reuploads its local bytes after server expiry", async () => { - const saved = { - ...image, - uploadedAttachmentId: "saved-image", - uploadEnvironmentId: environmentId, - }; - const reused = await prepareTurnAttachments({ - environmentId, - attachments: [saved], - supportsImageUploads: true, - }); - expect(reused.status === "ready" && reused.attachments[0]).toEqual({ - type: "image", - id: "saved-image", - name: image.name, - mimeType: image.mimeType, - sizeBytes: image.sizeBytes, - }); - expect(mocks.upload).not.toHaveBeenCalled(); - mocks.executeAtomQuery.mockResolvedValueOnce({ - _tag: "Failure", - error: { _tag: "AssetAttachmentNotFoundError" }, - }); - const restored = await prepareTurnAttachments({ - environmentId, - attachments: [saved], - supportsImageUploads: true, - }); - expect(restored.status === "ready" && restored.draftAttachments[0]).toEqual({ - ...saved, - uploadedAttachmentId: MINTED_ID, - }); - expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { - encoding: "base64", - }); - }); + }); + expect(mocks.upload).not.toHaveBeenCalled(); + mocks.executeAtomQuery.mockResolvedValueOnce({ + _tag: "Failure", + error: { _tag: "AssetAttachmentNotFoundError" }, + }); + const restored = yield* prepare({ + environmentId, + attachments: [saved], + supportsImageUploads: true, + }); + expect(restored.status === "ready" && restored.draftAttachments[0]).toEqual({ + ...saved, + uploadedAttachmentId: MINTED_ID, + }); + expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { + encoding: "base64", + }); + }), + ); - it("does not reuse an image upload from another environment", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [ - { - ...image, - uploadedAttachmentId: "other-image", - uploadEnvironmentId: EnvironmentId.make("other"), - }, - ], - supportsImageUploads: true, - }); - expect(mocks.executeAtomQuery).not.toHaveBeenCalled(); - expect(mocks.upload).toHaveBeenCalledOnce(); - expect(prepared.status === "ready" && prepared.draftAttachments[0]?.uploadEnvironmentId).toBe( - environmentId, - ); - }); + it.effect("does not reuse an image upload from another environment", () => + Effect.gen(function* () { + const prepared = yield* prepare({ + environmentId, + attachments: [ + { + ...image, + uploadedAttachmentId: "other-image", + uploadEnvironmentId: EnvironmentId.make("other"), + }, + ], + supportsImageUploads: true, + }); + expect(mocks.executeAtomQuery).not.toHaveBeenCalled(); + expect(mocks.upload).toHaveBeenCalledOnce(); + expect(prepared.status === "ready" && prepared.draftAttachments[0]?.uploadEnvironmentId).toBe( + environmentId, + ); + }), + ); - it("aborts an active transfer without dropping local bytes or stamping a partial upload", async () => { - const started = Promise.withResolvers(); - const controller = new AbortController(); - const persist = vi.fn(async () => "persisted" as const); - mocks.upload.mockImplementation( - (_uri: string, _url: string, options: { signal: AbortSignal }) => - new Promise((_, reject) => { - options.signal.addEventListener("abort", () => reject(new Error("cancelled")), { - once: true, - }); - started.resolve(); - }), - ); - const preparing = prepareTurnAttachments({ - environmentId, - attachments: [file], - signal: controller.signal, - persistUploadedReferences: persist, - }); - await started.promise; - controller.abort(); - expect(await preparing).toEqual({ status: "abandoned" }); - expect(persist).not.toHaveBeenCalled(); - expect(mocks.deleteFile).not.toHaveBeenCalled(); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); + it.effect( + "aborts an active transfer without dropping local bytes or stamping a partial upload", + () => + Effect.gen(function* () { + const started = Promise.withResolvers(); + const persist = vi.fn(() => Effect.succeed("persisted" as const)); + mocks.upload.mockImplementation( + (_uri: string, _url: string, options: { signal: AbortSignal }) => + new Promise((_, reject) => { + options.signal.addEventListener("abort", () => reject(new Error("cancelled")), { + once: true, + }); + started.resolve(); + }), + ); + const preparing = yield* Effect.forkChild( + prepare({ environmentId, attachments: [file], persistUploadedReferences: persist }), + ); + yield* Effect.promise(() => started.promise); + yield* Fiber.interrupt(preparing); + expect(Exit.hasInterrupts(yield* Fiber.await(preparing))).toBe(true); + expect(persist).not.toHaveBeenCalled(); + expect(mocks.deleteFile).not.toHaveBeenCalled(); + expect(removeCallsFor(MINTED_ID)).toBe(1); + }), + ); - it("removes pending uploads when the native HTTP request fails", async () => { - mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); + it.effect("removes pending uploads when the native HTTP request fails", () => + Effect.gen(function* () { + mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); - await expect(prepareTurnAttachments({ environmentId, attachments: [file] })).rejects.toThrow( - "Upload failed for 'report.pdf' (500).", - ); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [file] }))).message, + ).toContain("Upload failed for 'report.pdf' (500)."); + expect(removeCallsFor(MINTED_ID)).toBe(1); + }), + ); - it("keeps a previously persisted upload when a later attachment fails", async () => { - const previouslyUploaded = { - ...file, - id: "file-existing", - uploadedAttachmentId: "pending-existing-pdf", - uploadEnvironmentId: environmentId, - }; - mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); + it.effect("keeps a previously persisted upload when a later attachment fails", () => + Effect.gen(function* () { + const previouslyUploaded = { + ...file, + id: "file-existing", + uploadedAttachmentId: "pending-existing-pdf", + uploadEnvironmentId: environmentId, + }; + mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); - await expect( - prepareTurnAttachments({ environmentId, attachments: [previouslyUploaded, file] }), - ).rejects.toThrow("Upload failed for 'report.pdf' (500)."); + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [previouslyUploaded, file] }))) + .message, + ).toContain("Upload failed for 'report.pdf' (500)."); - expect(removeCallsFor("pending-existing-pdf")).toBe(0); - }); + expect(removeCallsFor("pending-existing-pdf")).toBe(0); + }), + ); }); describe("releasePendingAttachmentUploads", () => { diff --git a/apps/mobile/src/lib/attachmentUpload.ts b/apps/mobile/src/lib/attachmentUpload.ts index b129b840370d2..b4bf8b0aeb472 100644 --- a/apps/mobile/src/lib/attachmentUpload.ts +++ b/apps/mobile/src/lib/attachmentUpload.ts @@ -15,6 +15,7 @@ import { AuthOrchestrationOperateScope, PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES, } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import { appAtomRegistry } from "../state/atom-registry"; @@ -31,6 +32,7 @@ import { } from "./composerImages"; import { imageMimeType } from "@t3tools/shared/image"; import { uuidv4 } from "./uuid"; +import { type ComposerAttachmentUploadError, uploadError } from "./composerAttachmentUploadQueue"; export type UploadedMobileAttachment = | UploadChatImageAttachment @@ -323,65 +325,55 @@ export async function uploadComposerAttachment( /** * Turns a message's attachments into wire references. `uploadAttachment` is the shared upload - * registry, so a send joins an upload the composer already started. persistUploadedReferences - * runs only for new uploaded ids; save them to their owner so crash retries reuse bytes. + * registry, so a send joins an upload the composer already started; interrupting this stops + * waiting without cancelling that shared upload. persistUploadedReferences runs only for new + * uploaded ids; save them to their owner so crash retries reuse bytes. */ -export async function prepareTurnAttachments(input: { +export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(function* (input: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; readonly supportsImageUploads?: boolean; - readonly signal?: AbortSignal; - readonly uploadAttachment: (attachment: DraftComposerAttachment) => Promise; + readonly uploadAttachment: ( + attachment: DraftComposerAttachment, + ) => Effect.Effect; readonly persistUploadedReferences?: ( draftAttachments: ReadonlyArray, - ) => Promise<"persisted" | "abandon">; -}): Promise { + ) => Effect.Effect<"persisted" | "abandon">; +}) { const { environmentId } = input; - if (input.signal?.aborted) return { status: "abandoned" }; + const inlineImages = (attachments: ReadonlyArray) => + Effect.tryPromise({ + try: () => + toUploadChatImageAttachments( + attachments.filter( + (attachment): attachment is DraftComposerImageAttachment => attachment.type === "image", + ), + ), + catch: uploadError, + }); const files = input.attachments.filter((attachment) => attachment.type === "file"); - const ready = ( - attachments: ReadonlyArray, - pendingAttachmentIds: ReadonlyArray, - draftAttachments: ReadonlyArray, - ): PreparedTurnAttachments => ({ - status: "ready", - attachments, - draftAttachments, - pendingAttachmentIds, - }); - if (input.attachments.length === 0 || (files.length === 0 && !input.supportsImageUploads)) { - try { - const imageAttachments = await toUploadChatImageAttachments( - input.attachments.filter((attachment) => attachment.type === "image"), - ); - if (input.signal?.aborted) return { status: "abandoned" }; - return ready(imageAttachments, [], input.attachments); - } catch (error) { - if (input.signal?.aborted) return { status: "abandoned" }; - throw error; - } + const imageAttachments = yield* inlineImages(input.attachments); + return { + status: "ready", + attachments: imageAttachments, + draftAttachments: input.attachments, + pendingAttachmentIds: [], + } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult; } - requireUploadAccess(environmentId); + yield* Effect.try({ try: () => requireUploadAccess(environmentId), catch: uploadError }); const uploadedAttachments: UploadedMobileAttachment[] = []; const pendingAttachmentIds: string[] = []; - try { - for (const attachment of input.attachments) { - if (input.signal?.aborted) return { status: "abandoned" }; - if (attachment.type === "image" && !input.supportsImageUploads) { - uploadedAttachments.push(...(await toUploadChatImageAttachments([attachment]))); - continue; - } - const uploadedId = await input.uploadAttachment(attachment); - pendingAttachmentIds.push(uploadedId); - uploadedAttachments.push(uploadedReference(attachment, uploadedId)); + for (const attachment of input.attachments) { + if (attachment.type === "image" && !input.supportsImageUploads) { + uploadedAttachments.push(...(yield* inlineImages([attachment]))); + continue; } - } catch (error) { - if (input.signal?.aborted) return { status: "abandoned" }; - throw error; + const uploadedId = yield* input.uploadAttachment(attachment); + pendingAttachmentIds.push(uploadedId); + uploadedAttachments.push(uploadedReference(attachment, uploadedId)); } - if (input.signal?.aborted) return { status: "abandoned" }; const draftAttachments = withUploadedMobileAttachmentReferences({ environmentId, @@ -394,9 +386,14 @@ export async function prepareTurnAttachments(input: { if ( referencesChanged && input.persistUploadedReferences && - (await input.persistUploadedReferences(draftAttachments)) === "abandon" + (yield* input.persistUploadedReferences(draftAttachments)) === "abandon" ) { - return { status: "abandoned" }; + return { status: "abandoned" } as PrepareTurnAttachmentsResult; } - return ready(uploadedAttachments, pendingAttachmentIds, draftAttachments); -} + return { + status: "ready", + attachments: uploadedAttachments, + draftAttachments, + pendingAttachmentIds, + } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult; +}); diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts index 56cfa301df861..9a65e348a28d0 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts @@ -1,5 +1,8 @@ import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts"; -import { describe, expect, it, vi } from "vite-plus/test"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import { describe, expect, it } from "@effect/vitest"; +import { vi } from "vite-plus/test"; import { questionAttachmentDraftKey, questionAttachmentDraftPrefix, @@ -239,36 +242,38 @@ describe("composer attachment upload queue", () => { queue.dispose(); }); - it("joins a send to the running upload instead of uploading again", async () => { - const gate = Promise.withResolvers(); - const started = Promise.withResolvers(); - const uploaded = vi.fn(); - const upload = vi.fn(async () => { - started.resolve(); - return gate.promise; - }); - let states: Readonly> = {}; - const queue = createComposerAttachmentUploadQueue({ - upload, - onUploaded: uploaded, - onChange: (next) => { - states = next; - }, - }); - const local = request("video"); - queue.sync([local]); - await started.promise; - // Sending hands the attachment from the draft to the outbox, which uploads it too. - const sent = queue.upload(local); - queue.sync([]); - gate.resolve("uploaded-1"); - await expect(sent).resolves.toBe("uploaded-1"); - expect(upload).toHaveBeenCalledOnce(); - expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); - await queue.settled(); - expect(states).toEqual({}); - queue.dispose(); - }); + it.effect("joins a send to the running upload instead of uploading again", () => + Effect.gen(function* () { + const gate = Promise.withResolvers(); + const started = Promise.withResolvers(); + const uploaded = vi.fn(); + const upload = vi.fn(async () => { + started.resolve(); + return gate.promise; + }); + let states: Readonly> = {}; + const queue = createComposerAttachmentUploadQueue({ + upload, + onUploaded: uploaded, + onChange: (next) => { + states = next; + }, + }); + const local = request("video"); + queue.sync([local]); + yield* Effect.promise(() => started.promise); + // Sending hands the attachment from the draft to the outbox, which uploads it too. + const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true }); + queue.sync([]); + gate.resolve("uploaded-1"); + expect(yield* Fiber.join(sent)).toBe("uploaded-1"); + expect(upload).toHaveBeenCalledOnce(); + expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); + yield* Effect.promise(() => queue.settled()); + expect(states).toEqual({}); + queue.dispose(); + }), + ); }); describe("draft upload scope and offline submission", () => { diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index fe3836ff4c256..8e94ab3054bef 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -289,12 +289,12 @@ export function createComposerAttachmentUploadQueue(options: { return { sync, /** Uploads the attachment, joining its running upload when there is one. */ - upload(request: ComposerAttachmentUploadRequest, signal?: AbortSignal): Promise { - if (disposed) return Promise.reject(new Error("Upload cancelled.")); - return Effect.runPromise( - RcMap.get(uploads, UploadKey.of(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped), - signal ? { signal } : undefined, - ); + upload( + request: ComposerAttachmentUploadRequest, + ): Effect.Effect { + return disposed + ? Effect.fail(uploadError(new Error("Upload cancelled."))) + : RcMap.get(uploads, UploadKey.of(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped); }, retry(environmentId: EnvironmentId, attachmentId: string) { const id = composerAttachmentUploadKey(environmentId, attachmentId); @@ -318,14 +318,17 @@ export function createComposerAttachmentUploadQueue(options: { }; } -function uploadError(cause: unknown): ComposerAttachmentUploadError { +export function uploadError(cause: unknown): ComposerAttachmentUploadError { + if (isComposerAttachmentUploadError(cause)) return cause; return new ComposerAttachmentUploadError({ message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.", cause, }); } -class ComposerAttachmentUploadError extends Schema.TaggedError()( +export class ComposerAttachmentUploadError extends Schema.TaggedError()( "ComposerAttachmentUploadError", { message: Schema.String, cause: Schema.Defect() }, ) {} + +const isComposerAttachmentUploadError = Schema.is(ComposerAttachmentUploadError); diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index a4ecf51dbccd8..a658b1c169c08 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -243,9 +243,8 @@ function composerAttachmentUploads() { export function uploadComposerAttachmentShared( environmentId: EnvironmentId, attachment: DraftComposerAttachment, - signal?: AbortSignal, -): Promise { - return composerAttachmentUploads().upload({ environmentId, attachment }, signal); +) { + return composerAttachmentUploads().upload({ environmentId, attachment }); } /** diff --git a/apps/mobile/src/state/thread-outbox-model.ts b/apps/mobile/src/state/thread-outbox-model.ts index 64fe62d8a15f7..59427907c4ebf 100644 --- a/apps/mobile/src/state/thread-outbox-model.ts +++ b/apps/mobile/src/state/thread-outbox-model.ts @@ -288,6 +288,9 @@ export function shouldRetryThreadOutboxDelivery(error: unknown): boolean { case "EnvironmentRpcUnavailableError": case "EnvironmentNotRegisteredError": return true; + // A shared upload's failure decides by what made the upload fail. + case "ComposerAttachmentUploadError": + return "cause" in error && shouldRetryThreadOutboxDelivery(error.cause); default: break; } diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index af67248c31c1a..b9ad37a6d7d0d 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -7,6 +7,8 @@ import { ProviderInstanceId, ThreadId, } from "@t3tools/contracts"; +import { uploadError } from "../lib/composerAttachmentUploadQueue"; +import * as Effect from "effect/Effect"; import * as Cause from "effect/Cause"; import { AsyncResult, type Atom } from "effect/reactivity"; import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; @@ -230,12 +232,14 @@ beforeEach(() => { command.mockReset().mockResolvedValue(AsyncResult.success(undefined)); } state.start.mockResolvedValue(AsyncResult.success({})); - state.prepare.mockReset().mockImplementation(async ({ attachments }) => ({ - status: "ready", - attachments: [], - draftAttachments: attachments, - pendingAttachmentIds: [], - })); + state.prepare.mockReset().mockImplementation(({ attachments }) => + Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: attachments, + pendingAttachmentIds: [], + }), + ); }); describe("queued PR checkout", () => { @@ -486,16 +490,19 @@ describe("queued task operation access", () => { : {}, ); await state.manager.enqueue(queued); - state.prepare.mockImplementationOnce(async ({ attachments }) => { - setOperateGrant("secondary", false); - if (uploadFails) throw new Error("This connection cannot upload attachments."); - return { - status: "ready", - attachments: [], - draftAttachments: attachments, - pendingAttachmentIds: [], - }; - }); + state.prepare.mockImplementationOnce(({ attachments }) => + Effect.suspend(() => { + setOperateGrant("secondary", false); + return uploadFails + ? Effect.fail(uploadError(new Error("This connection cannot upload attachments."))) + : Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: attachments, + pendingAttachmentIds: [], + }); + }), + ); await runDrain(); expect(state.prepare).toHaveBeenCalledOnce(); expect(state.start).not.toHaveBeenCalled(); diff --git a/apps/mobile/src/state/threadOutboxDelivery.test.ts b/apps/mobile/src/state/threadOutboxDelivery.test.ts index 8800757ab5d56..b85ed8e4bf4cd 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.test.ts @@ -2,6 +2,7 @@ import { acknowledgedThreadMessagesAtom, pruneCaughtUpAcknowledgedThreadMessages, } from "./acknowledged-thread-messages"; +import * as Effect from "effect/Effect"; import { CommandId, EnvironmentId, @@ -242,12 +243,14 @@ describe("thread outbox attachment preparation", () => { fileUri: "file:///documents/keep.pdf", }); await harness.manager.enqueue(message); - harness.prepareTurnAttachments.mockResolvedValueOnce({ - status: "ready", - attachments: [], - draftAttachments: message.attachments, - pendingAttachmentIds: [], - }); + harness.prepareTurnAttachments.mockReturnValueOnce( + Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: message.attachments, + pendingAttachmentIds: [], + }), + ); harness.canOperate = false; await expect(prepareQueuedMessageAttachments(message)).resolves.toEqual({ status: "abandoned", @@ -267,10 +270,12 @@ describe("thread outbox attachment preparation", () => { ); const preparationStarted = Promise.withResolvers(); const preparationBarrier = Promise.withResolvers(); - harness.prepareTurnAttachments.mockImplementationOnce(async () => { - preparationStarted.resolve(); - return preparationBarrier.promise; - }); + harness.prepareTurnAttachments.mockImplementationOnce(() => + Effect.promise(() => { + preparationStarted.resolve(); + return preparationBarrier.promise; + }), + ); await harness.manager.enqueue(message); appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); @@ -305,15 +310,17 @@ describe("thread outbox attachment preparation", () => { } : attachment, ); - harness.prepareTurnAttachments.mockImplementationOnce(async (input) => { - expect(await input.persistUploadedReferences?.(uploadedAttachments)).toBe("persisted"); - return { - status: "ready", - attachments: [], - draftAttachments: uploadedAttachments, - pendingAttachmentIds: ["pending-new-upload"], - }; - }); + harness.prepareTurnAttachments.mockImplementationOnce((input) => + Effect.gen(function* () { + expect(yield* input.persistUploadedReferences!(uploadedAttachments)).toBe("persisted"); + return { + status: "ready", + attachments: [], + draftAttachments: uploadedAttachments, + pendingAttachmentIds: ["pending-new-upload"], + } as const; + }), + ); await harness.manager.enqueue(message); const revision = harness.manager.revisionOf(message.messageId); diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index 79e0ceced038c..3a3802d425897 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -23,6 +23,7 @@ import { } from "@t3tools/contracts"; import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git"; import * as Cause from "effect/Cause"; +import * as Effect from "effect/Effect"; import { AsyncResult, Atom } from "effect/reactivity"; import { createDebugLogger } from "../lib/debugLog"; @@ -230,25 +231,28 @@ export async function prepareQueuedMessageAttachments(queuedMessage: QueuedThrea } let persistedMessage = queuedMessage; let deliveryRevision = revision; - const result = await prepareTurnAttachments({ - environmentId: queuedMessage.environmentId, - attachments: queuedMessage.attachments, - supportsImageUploads: true, - uploadAttachment: (attachment) => - uploadComposerAttachmentShared(queuedMessage.environmentId, attachment), - persistUploadedReferences: async (draftAttachments) => { - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - return "abandon"; - } - const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; - if (!(await updateThreadOutboxMessage(updatedMessage, revision))) { - return "abandon"; - } - persistedMessage = updatedMessage; - deliveryRevision = revision + 1; - return "persisted"; - }, - }); + const result = await Effect.runPromise( + prepareTurnAttachments({ + environmentId: queuedMessage.environmentId, + attachments: queuedMessage.attachments, + supportsImageUploads: true, + uploadAttachment: (attachment) => + uploadComposerAttachmentShared(queuedMessage.environmentId, attachment), + persistUploadedReferences: (draftAttachments) => + Effect.promise(async () => { + if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { + return "abandon" as const; + } + const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; + if (!(await updateThreadOutboxMessage(updatedMessage, revision))) { + return "abandon" as const; + } + persistedMessage = updatedMessage; + deliveryRevision = revision + 1; + return "persisted" as const; + }), + }), + ); if ( result.status === "abandoned" || !isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index bde1d00c5da99..0f0e4a69419fb 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -1,4 +1,6 @@ import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import { createEnvironmentThreadShellAtoms } from "@t3tools/client-runtime/state/threads"; import { applyShellStreamEvent, @@ -1213,65 +1215,67 @@ describe("mobile composer drafts", () => { expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledExactlyOnceWith(image.fileUri); }); - it("keeps an image through an inline read after its draft is removed", async () => { - const { prepareTurnAttachments } = - await vi.importActual("../lib/attachmentUpload"); - const image = { - id: "image-reading", - type: "image" as const, - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - fileUri: "file:///documents/t3-composer-attachments/photo.png", - previewUri: "file:///documents/t3-composer-attachments/photo.png", - }; - const readStarted = Promise.withResolvers(); - const read = Promise.withResolvers(); - const removed = Promise.withResolvers(); - let imageExists = true; - composerDraftFileMocks.readImage.mockImplementation(async () => { - readStarted.resolve(); - await read.promise; - if (!imageExists) throw new Error("The image was deleted during its read"); - return "YWJj"; - }); - composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => { - imageExists = false; - removed.resolve(); - }); - appendComposerDraftAttachments("environment-1:thread-1", [image]); - await flushComposerDrafts(); - // Without image uploads the image is inlined, so nothing goes through the upload registry. - const preparing = prepareTurnAttachments({ - environmentId: EnvironmentId.make("environment-1"), - attachments: [image], - uploadAttachment: vi.fn(), - }); - onTestFinished(async () => { - read.resolve(); - await preparing.catch(() => undefined); - }); - await readStarted.promise; - clearComposerDraftContent("environment-1:thread-1", { deferAttachmentCleanup: true }); - await releaseUnusedComposerAttachmentFiles([image]); - expect(imageExists).toBe(true); - - read.resolve(); - const prepared = await preparing; - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "image", + it.effect("keeps an image through an inline read after its draft is removed", () => + Effect.gen(function* () { + const { prepareTurnAttachments } = yield* Effect.promise(() => + vi.importActual("../lib/attachmentUpload"), + ); + const image = { + id: "image-reading", + type: "image" as const, name: "photo.png", mimeType: "image/png", sizeBytes: 3, - dataUrl: "data:image/png;base64,YWJj", - }, - ]); - await removed.promise; - expect(imageExists).toBe(false); - }); + fileUri: "file:///documents/t3-composer-attachments/photo.png", + previewUri: "file:///documents/t3-composer-attachments/photo.png", + }; + const readStarted = Promise.withResolvers(); + const read = Promise.withResolvers(); + const removed = Promise.withResolvers(); + let imageExists = true; + composerDraftFileMocks.readImage.mockImplementation(async () => { + readStarted.resolve(); + await read.promise; + if (!imageExists) throw new Error("The image was deleted during its read"); + return "YWJj"; + }); + composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => { + imageExists = false; + removed.resolve(); + }); + appendComposerDraftAttachments("environment-1:thread-1", [image]); + yield* Effect.promise(() => flushComposerDrafts()); + // Without image uploads the image is inlined, so nothing goes through the upload registry. + const preparing = yield* Effect.forkChild( + prepareTurnAttachments({ + environmentId: EnvironmentId.make("environment-1"), + attachments: [image], + uploadAttachment: vi.fn(), + }), + ); + onTestFinished(() => read.resolve()); + yield* Effect.promise(() => readStarted.promise); + clearComposerDraftContent("environment-1:thread-1", { deferAttachmentCleanup: true }); + yield* Effect.promise(() => releaseUnusedComposerAttachmentFiles([image])); + expect(imageExists).toBe(true); + + read.resolve(); + const prepared = yield* Fiber.join(preparing); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,YWJj", + }, + ]); + yield* Effect.promise(() => removed.promise); + expect(imageExists).toBe(false); + }), + ); it("keeps a failed-send draft's pending upload for retry", async () => { const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true); diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index d9eb8a3937c27..f8f836dd77e9b 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -1,4 +1,5 @@ import { useComposerAttachmentActions } from "./use-composer-attachment-actions"; +import * as Effect from "effect/Effect"; import * as Dialog from "../components/ui/Dialog"; import { AuthOrchestrationOperateScope } from "@t3tools/contracts"; import { readEnvironmentScope } from "./session"; @@ -511,13 +512,15 @@ export function useThreadComposerState(input: { setIsSavingQueuedEdit(true); const capabilities = selectedEnvironmentRuntime?.serverConfig?.environment.capabilities; const prepared = await settlePromise(() => - prepareTurnAttachments({ - environmentId: thread.environmentId, - attachments: draft.attachments, - supportsImageUploads: capabilities !== undefined, - uploadAttachment: (attachment) => - uploadComposerAttachmentShared(thread.environmentId, attachment), - }), + Effect.runPromise( + prepareTurnAttachments({ + environmentId: thread.environmentId, + attachments: draft.attachments, + supportsImageUploads: capabilities !== undefined, + uploadAttachment: (attachment) => + uploadComposerAttachmentShared(thread.environmentId, attachment), + }), + ), ); if (AsyncResult.isFailure(prepared)) { savingQueuedEditRef.current = false; From cec4291a8f02dd0e9d956304ff5041f9553ebdbe Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 11:04:40 -0800 Subject: [PATCH 5/5] refactor(mobile): record uploads and settle transfers in Effect Recording a finished upload on drafts and queued messages is an Effect.fn, and the registry's onUploaded is an Effect. An interrupted native transfer aborts and waits for its request to settle as part of its interruption, and a re-added attachment waits on that fiber, which replaces the promise map of settling transfers. Co-Authored-By: Claude Opus 5.5 --- .../src/lib/composerAttachmentUploadQueue.ts | 80 +++++++++---------- .../src/state/composer-attachment-uploads.ts | 53 +++++++----- 2 files changed, 69 insertions(+), 64 deletions(-) diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index 8e94ab3054bef..f52b6fcdbe36b 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -169,11 +169,11 @@ export function createComposerAttachmentUploadQueue(options: { signal: AbortSignal, onProgress: (progress: number) => void, ) => Promise; - /** Saves a finished upload's id; the upload is ready once this resolves. */ + /** Saves a finished upload's id; the upload is ready once this succeeds. */ readonly onUploaded?: ( request: ComposerAttachmentUploadRequest, uploadedId: string, - ) => Promise; + ) => Effect.Effect; readonly onChange: (states: Readonly>) => void; }) { let states: Readonly> = {}; @@ -194,56 +194,44 @@ export function createComposerAttachmentUploadQueue(options: { } const permits = Semaphore.makeUnsafe(3); - // An interrupted transfer settles before its attachment can upload again. - const settling = new Map>(); const running = new Set>(); + // The latest transfer per attachment: a re-added attachment waits for an aborted one to settle. + const transfers = new Map>(); - const transfer = (key: UploadKey) => + /** The native transfer; interrupting it aborts the request and waits for it to settle. */ + const nativeTransfer = (key: UploadKey) => Effect.suspend(() => { - const previous = settling.get(key.id); const controller = new AbortController(); - let done: Promise | undefined; - const start = () => { - if (states[key.id]?.status !== "uploading") - setState(key.id, { status: "uploading", progress: 0 }); - done = options.upload(key.request!, controller.signal, (progress) => { - if (controller.signal.aborted) return; - setState(key.id, { - status: "uploading", - progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, - }); + if (states[key.id]?.status !== "uploading") { + setState(key.id, { status: "uploading", progress: 0 }); + } + const done = options.upload(key.request!, controller.signal, (progress) => { + if (controller.signal.aborted) return; + setState(key.id, { + status: "uploading", + progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, }); - return done; - }; - return Effect.promise(() => previous ?? Promise.resolve()).pipe( - Effect.andThen(Effect.tryPromise({ try: start, catch: uploadError })), - Semaphore.withPermits(permits, 1), - Effect.tap((uploadedId) => - Effect.tryPromise({ - try: () => options.onUploaded?.(key.request!, uploadedId) ?? Promise.resolve(), - catch: uploadError, - }), - ), - Effect.tap(() => Effect.sync(() => setState(key.id, { status: "ready" }))), - Effect.tapError((error) => - Effect.sync(() => setState(key.id, { status: "failed", reason: error.message })), - ), + }); + return Effect.tryPromise({ try: () => done, catch: uploadError }).pipe( Effect.onInterrupt(() => - Effect.sync(() => { - controller.abort(); - const settled = (done ?? Promise.resolve("")).then( - () => undefined, - () => undefined, - ); - settling.set(key.id, settled); - void settled.then(() => { - if (settling.get(key.id) === settled) settling.delete(key.id); - }); - }), + Effect.sync(() => controller.abort()).pipe( + Effect.andThen(Effect.tryPromise({ try: () => done, catch: uploadError })), + Effect.ignore, + ), ), ); }); + const transfer = (key: UploadKey, previous: Fiber.Fiber | undefined) => + (previous === undefined ? Effect.void : Fiber.await(previous)).pipe( + Effect.andThen(nativeTransfer(key).pipe(Semaphore.withPermits(permits, 1))), + Effect.tap((uploadedId) => options.onUploaded?.(key.request!, uploadedId) ?? Effect.void), + Effect.tap(() => Effect.sync(() => setState(key.id, { status: "ready" }))), + Effect.tapError((error) => + Effect.sync(() => setState(key.id, { status: "failed", reason: error.message })), + ), + ); + const scope = Scope.makeUnsafe(); const uploads = Effect.runSync( Scope.provide(scope)( @@ -251,9 +239,13 @@ export function createComposerAttachmentUploadQueue(options: { lookup: Effect.fn("ComposerAttachmentUploads.lookup")(function* (key: UploadKey) { setState(key.id, { status: "uploading", progress: 0 }); yield* Effect.addFinalizer(() => Effect.sync(() => setState(key.id, undefined))); - const fiber = yield* Effect.forkScoped(transfer(key)); + const fiber = yield* Effect.forkScoped(transfer(key, transfers.get(key.id))); + transfers.set(key.id, fiber); running.add(fiber); - fiber.addObserver(() => running.delete(fiber)); + fiber.addObserver(() => { + running.delete(fiber); + if (transfers.get(key.id) === fiber) transfers.delete(key.id); + }); return fiber; }), }), diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index a658b1c169c08..71812bb70aa72 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -1,5 +1,6 @@ import { useAtomValue } from "@effect/atom-react"; import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { Atom } from "effect/reactivity"; @@ -10,6 +11,7 @@ import { type DraftComposerAttachment, } from "../lib/composerImages"; import { + uploadError, composerAttachmentUploadKey, composerDraftEnvironmentId, canUploadComposerAttachment, @@ -134,10 +136,10 @@ function withUploadedAttachment( }; } -async function recordQueuedAttachmentUpload( +const recordQueuedAttachmentUpload = Effect.fn("ComposerAttachments.recordQueuedUpload")(function* ( environmentId: EnvironmentId, uploaded: DraftComposerAttachment, -): Promise { +) { const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); const holders = flattenQueuedThreadMessages( appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), @@ -147,17 +149,22 @@ async function recordQueuedAttachmentUpload( !editing[message.messageId] && withUploadedAttachment(message, uploaded) !== null, ); - await Promise.all( - holders.map((message) => - // An editor that opened meanwhile owns the message; its draft records the upload. - modifyThreadOutboxMessage(message.messageId, (current) => - appAtomRegistry.get(editingQueuedMessageIdsAtom)[current.messageId] - ? null - : withUploadedAttachment(current, uploaded), - ), - ), + yield* Effect.forEach( + holders, + (message) => + Effect.tryPromise({ + // An editor that opened meanwhile owns the message; its draft records the upload. + try: () => + modifyThreadOutboxMessage(message.messageId, (current) => + appAtomRegistry.get(editingQueuedMessageIdsAtom)[current.messageId] + ? null + : withUploadedAttachment(current, uploaded), + ), + catch: uploadError, + }), + { concurrency: "unbounded", discard: true }, ); -} +}); // Each connected environment's session is read here so a granted or revoked // operate scope re-syncs the queue. @@ -189,10 +196,10 @@ const composerAttachmentUploadRequestsAtom = Atom.make((get) => { }).pipe(Atom.withLabel("mobile:composer-attachment-upload-requests")); /** Saves a finished upload's id on every draft and queued message holding the attachment. */ -async function recordComposerAttachmentUpload( +const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUpload")(function* ( request: ComposerAttachmentUploadRequest, uploadedId: string, -): Promise { +) { const { environmentId, attachment } = request; const uploaded: DraftComposerAttachment = { ...attachment, @@ -214,14 +221,20 @@ async function recordComposerAttachmentUpload( draftsChanged = setComposerDraftAttachmentUpload(key, uploaded) || draftsChanged; } } - await recordQueuedAttachmentUpload(environmentId, uploaded); + yield* recordQueuedAttachmentUpload(environmentId, uploaded); if (!draftsChanged) return; // A failed draft save only costs a re-upload after restart; the outbox saves its own references. - await flushComposerDrafts().catch((error: unknown) => { - if (!isComposerDraftPersistenceError(error)) throw error; - console.warn("[composer-attachments] could not save the uploaded reference", error); - }); -} + yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: uploadError }).pipe( + Effect.catchIf( + (error) => isComposerDraftPersistenceError(error.cause), + (error) => + Effect.logWarning( + "[composer-attachments] could not save the uploaded reference", + error.cause, + ), + ), + ); +}); function composerAttachmentUploads() { uploadQueue ??= createComposerAttachmentUploadQueue({