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 diff --git a/apps/mobile/src/lib/attachmentUpload.test.ts b/apps/mobile/src/lib/attachmentUpload.test.ts index 0097ab7ed77c0..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,11 +90,51 @@ 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, + prepareTurnAttachments as prepareWithUploads, releasePendingAttachmentUploads, + uploadComposerAttachment, withUploadedMobileAttachmentReferences, } from "./attachmentUpload"; + +/** Uploads each attachment directly, as the shared registry does for one holder. */ +function prepare( + input: Omit[0], "uploadAttachment"> & { + readonly onUploadProgress?: (attachmentId: string, progress: number) => void; + }, +) { + return prepareWithUploads({ + ...input, + // An interrupted send waits for its transfer to settle, as the shared registry does. + uploadAttachment: (attachment) => + 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"; const environmentId = EnvironmentId.make("environment-1"); @@ -176,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 () => { @@ -250,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( @@ -533,215 +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, - }; - - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [previouslyUploaded, image], - }); + it.effect("reuses a pending file upload from a previous outbox attempt", () => + Effect.gen(function* () { + const previouslyUploaded = { + ...file, + uploadedAttachmentId: "pending-existing-pdf", + uploadEnvironmentId: environmentId, + }; - 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(); - }); + const prepared = yield* prepare({ + environmentId, + attachments: [previouslyUploaded, image], + }); - 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], - }); + 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(mocks.upload).toHaveBeenCalledOnce(); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); - }); + 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, + }; - 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([ - { - 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); - }); + const prepared = yield* prepare({ + environmentId, + attachments: [previouslyUploaded], + }); - 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).toHaveBeenCalledOnce(); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); + }), + ); - it("does not reuse an image upload from another environment", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [ + 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([ { - ...image, - uploadedAttachmentId: "other-image", - uploadEnvironmentId: EnvironmentId.make("other"), + type: "image", + id: MINTED_ID, + name: image.name, + mimeType: image.mimeType, + sizeBytes: image.sizeBytes, }, - ], - supportsImageUploads: true, - }); - expect(mocks.executeAtomQuery).not.toHaveBeenCalled(); - expect(mocks.upload).toHaveBeenCalledOnce(); - expect(prepared.status === "ready" && prepared.draftAttachments[0]?.uploadEnvironmentId).toBe( - environmentId, - ); - }); + ]); + expect(prepared.draftAttachments).toEqual([ + { ...image, uploadedAttachmentId: MINTED_ID, uploadEnvironmentId: environmentId }, + ]); + expect(persisted).toHaveBeenCalledWith(prepared.draftAttachments); + }), + ); - 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("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: "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 = 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("removes pending uploads when the native HTTP request fails", async () => { - mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); + 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, + ); + }), + ); - await expect(prepareTurnAttachments({ environmentId, attachments: [file] })).rejects.toThrow( - "Upload failed for 'report.pdf' (500).", - ); - 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("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("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: [previouslyUploaded, file] }), - ).rejects.toThrow("Upload failed for 'report.pdf' (500)."); + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [file] }))).message, + ).toContain("Upload failed for 'report.pdf' (500)."); + expect(removeCallsFor(MINTED_ID)).toBe(1); + }), + ); - expect(removeCallsFor("pending-existing-pdf")).toBe(0); - }); + 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: {} }); - it("deletes the minted uploads when the owner abandons the send", async () => { - const result = await prepareTurnAttachments({ - environmentId, - attachments: [file], - persistUploadedReferences: async () => "abandon", - }); + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [previouslyUploaded, file] }))) + .message, + ).toContain("Upload failed for 'report.pdf' (500)."); - expect(result.status).toBe("abandoned"); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); + 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 57699af22819e..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 @@ -248,159 +250,150 @@ async function uploadFileBytes( } } -/** persistUploadedReferences runs only for new uploaded ids; save them to their owner so crash retries reuse bytes. */ -export async function prepareTurnAttachments(input: { +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; 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 const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(function* (input: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; readonly supportsImageUploads?: boolean; - readonly signal?: AbortSignal; - readonly onUploadProgress?: (attachmentId: string, progress: number) => void; + 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 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."); + const imageAttachments = yield* inlineImages(input.attachments); + return { + status: "ready", + attachments: imageAttachments, + draftAttachments: input.attachments, + pendingAttachmentIds: [], + } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult; } + yield* Effect.try({ try: () => requireUploadAccess(environmentId), catch: uploadError }); 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 (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)); + for (const attachment of input.attachments) { + if (attachment.type === "image" && !input.supportsImageUploads) { + uploadedAttachments.push(...(yield* inlineImages([attachment]))); + continue; } + const uploadedId = yield* 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" }; - throw error; - } finally { - input.signal?.removeEventListener("abort", abort); + const draftAttachments = withUploadedMobileAttachmentReferences({ + environmentId, + attachments: input.attachments, + uploadedAttachments, + }); + const referencesChanged = draftAttachments.some( + (attachment, index) => attachment !== input.attachments[index], + ); + if ( + referencesChanged && + input.persistUploadedReferences && + (yield* input.persistUploadedReferences(draftAttachments)) === "abandon" + ) { + return { status: "abandoned" } as PrepareTurnAttachmentsResult; } -} + 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 6de39c413d49e..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, @@ -33,7 +36,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 +44,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 +60,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 +79,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 +97,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 +110,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 +137,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 +152,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 +165,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 +192,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 +211,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,21 +235,45 @@ 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(); - expect(upload).toHaveBeenCalledOnce(); - 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 0a28fff2231d5..f52b6fcdbe36b 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 succeeds. */ + readonly onUploaded?: ( + request: ComposerAttachmentUploadRequest, + uploadedId: string, + ) => Effect.Effect; 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,134 @@ 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); + const running = new Set>(); + // The latest transfer per attachment: a re-added attachment waits for an aborted one to settle. + const transfers = new Map>(); + + /** The native transfer; interrupting it aborts the request and waits for it to settle. */ + const nativeTransfer = (key: UploadKey) => + Effect.suspend(() => { 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, - }); - }), - ) - .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.", - }); - } - }) - .finally(() => { - jobs.delete(key); - pump(); + 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, }); - jobs.set(key, { controller, done }); + }); + return Effect.tryPromise({ try: () => done, catch: uploadError }).pipe( + Effect.onInterrupt(() => + 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)( + 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, transfers.get(key.id))); + transfers.set(key.id, fiber); + running.add(fiber); + fiber.addObserver(() => { + running.delete(fiber); + if (transfers.get(key.id) === fiber) transfers.delete(key.id); + }); + 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, - ]), - ); - 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(); + sync, + /** Uploads the attachment, joining its running upload when there is one. */ + 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 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); }, }; } + +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, + }); +} + +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.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..71812bb70aa72 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -1,16 +1,17 @@ -import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; 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, 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, type DraftComposerAttachment, } from "../lib/composerImages"; import { + uploadError, composerAttachmentUploadKey, composerDraftEnvironmentId, canUploadComposerAttachment, @@ -22,8 +23,7 @@ import { environmentServerConfigsAtom } from "./server"; import { flattenQueuedThreadMessages, threadOutboxManager, - threadOutboxRevision, - updateThreadOutboxMessage, + modifyThreadOutboxMessage, type QueuedThreadMessage, } from "./thread-outbox"; import { editingQueuedMessageIdsAtom } from "./use-thread-outbox"; @@ -136,33 +136,35 @@ function withUploadedAttachment( }; } -async function recordQueuedAttachmentUpload( +const recordQueuedAttachmentUpload = Effect.fn("ComposerAttachments.recordQueuedUpload")(function* ( 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; -} +) { + const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); + const holders = flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ).filter( + (message) => + message.environmentId === environmentId && + !editing[message.messageId] && + withUploadedAttachment(message, uploaded) !== null, + ); + 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. @@ -193,8 +195,73 @@ 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. */ +const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUpload")(function* ( + request: ComposerAttachmentUploadRequest, + uploadedId: string, +) { + 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; + } + } + yield* recordQueuedAttachmentUpload(environmentId, uploaded); + if (!draftsChanged) return; + // A failed draft save only costs a re-upload after restart; the outbox saves its own references. + 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({ + 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, +) { + return composerAttachmentUploads().upload({ environmentId, attachment }); +} + /** - * 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 +270,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/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-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/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); } diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index 263c48195d75d..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"; @@ -107,6 +109,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"); @@ -229,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", () => { @@ -485,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 f7d67f4349397..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, @@ -84,6 +85,7 @@ vi.mock("../lib/uuid", () => ({ vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: harness.prepareTurnAttachments, })); +vi.mock("./composer-attachment-uploads", () => ({ uploadComposerAttachmentShared: vi.fn() })); vi.mock("./projects", () => ({ environmentProjects: {} })); @@ -241,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", @@ -266,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 }); @@ -304,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 74cf7597e0ef7..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"; @@ -49,6 +50,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, @@ -229,23 +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, - 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 200b2b165c6b5..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,63 +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(); - const preparing = prepareTurnAttachments({ - environmentId: EnvironmentId.make("environment-1"), - attachments: [image], - }); - 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 08b9e20b27f60..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"; @@ -72,6 +73,7 @@ import { pendingThreadCreationMessage } from "./pending-thread-creation"; import { composerAttachmentUploadBlockReason, composerAttachmentUploadsAtom, + uploadComposerAttachmentShared, } from "./composer-attachment-uploads"; import { appendComposerDraftAttachments, @@ -510,11 +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, - }), + 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;