The best way to control Claude Code, Codex and more from any computer or phone
refactor(client-runtime): share the Effect attachment upload registry with web (#37743)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 16, 2026 at 12:15 UTC
1da6b14e92218f8b378d95c62223c4ca37b020dc
15 files changed
+1038
-907
apps/mobile/src/lib/attachmentUpload.test.ts
+7
-24
@@ -93,7 +93,6 @@ vi.mock("expo-file-system", () => ({
93
import * as Effect from "effect/Effect";
94
import * as Exit from "effect/Exit";
95
import * as Fiber from "effect/Fiber";
96
-import { uploadError } from "./composerAttachmentUploadQueue";
96
import {
97
prepareTurnAttachments as prepareWithUploads,
98
releasePendingAttachmentUploads,
@@ -109,30 +108,14 @@ function prepare(
108
) {
109
return prepareWithUploads({
110
...input,
112
- // An interrupted send waits for its transfer to settle, as the shared registry does.
111
uploadAttachment: (attachment) =>
114
- Effect.suspend(() => {
115
- const controller = new AbortController();
116
- const upload = uploadComposerAttachment(
117
- input.environmentId,
118
- attachment,
119
- controller.signal,
120
- input.onUploadProgress
121
- ? (progress) => input.onUploadProgress?.(attachment.id, progress)
122
- : undefined,
123
- );
124
- return Effect.tryPromise({ try: () => upload, catch: uploadError }).pipe(
125
- Effect.onInterrupt(() =>
126
- Effect.promise(() => {
127
- controller.abort();
128
- return upload.then(
129
- () => undefined,
130
- () => undefined,
131
- );
132
- }),
133
- ),
134
- );
135
- }),
112
+ uploadComposerAttachment(
113
+ input.environmentId,
114
+ attachment,
115
+ input.onUploadProgress
116
+ ? (progress) => input.onUploadProgress?.(attachment.id, progress)
117
+ : undefined,
118
+ ),
119
});
120
}
121
import type { DraftComposerAttachment } from "./composerImages";
apps/mobile/src/lib/attachmentUpload.ts
+49
-32
@@ -32,7 +32,10 @@ import {
32
} from "./composerImages";
33
import { imageMimeType } from "@t3tools/shared/image";
34
import { uuidv4 } from "./uuid";
35
-import { type ComposerAttachmentUploadError, uploadError } from "./composerAttachmentUploadQueue";
35
+import {
36
+ attachmentUploadError,
37
+ type AttachmentUploadError,
38
+} from "@t3tools/client-runtime/state/attachment-uploads";
39
40
export type UploadedMobileAttachment =
41
| UploadChatImageAttachment
@@ -257,40 +260,46 @@ function requireUploadAccess(environmentId: EnvironmentId): void {
260
}
261
262
/**
260
- * Uploads one attachment and resolves to its uploaded id, reusing an earlier upload the server
261
- * still has. An abort deletes the pending upload it created.
263
+ * Uploads one attachment and returns its uploaded id, reusing an earlier upload the server
264
+ * still has. Interrupting it aborts the native transfer and deletes the pending upload it minted;
265
+ * a failed transfer deletes it too.
266
*/
263
-export async function uploadComposerAttachment(
267
+export const uploadComposerAttachment = Effect.fn("uploadComposerAttachment")(function* (
268
environmentId: EnvironmentId,
269
attachment: DraftComposerAttachment,
266
- signal: AbortSignal,
270
onProgress?: (progress: number) => void,
268
-): Promise<string> {
269
- requireUploadAccess(environmentId);
271
+) {
272
+ yield* Effect.try({
273
+ try: () => requireUploadAccess(environmentId),
274
+ catch: attachmentUploadError,
275
+ });
276
if (
277
attachment.uploadEnvironmentId === environmentId &&
278
attachment.uploadedAttachmentId !== undefined
279
) {
274
- const verification = await verifyPersistedAttachmentUpload({
280
+ const verification = yield* verifyPersistedAttachmentUpload({
281
registry: appAtomRegistry,
282
createAssetUrl: assetEnvironment.createUrl,
283
environmentId,
284
attachmentId: attachment.uploadedAttachmentId,
285
});
280
- if (verification.status === "failed") throw verification.error;
286
+ if (verification.status === "failed") {
287
+ return yield* attachmentUploadError(verification.error);
288
+ }
289
if (verification.status === "verified") return attachment.uploadedAttachmentId;
290
}
283
- if (signal.aborted) throw new Error("Upload cancelled.");
284
- requireUploadAccess(environmentId);
291
+ yield* Effect.try({
292
+ try: () => requireUploadAccess(environmentId),
293
+ catch: attachmentUploadError,
294
+ });
295
if (
296
Option.isNone(
297
appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
298
)
299
) {
290
- throw new Error("The environment is not connected.");
300
+ return yield* attachmentUploadError(new Error("The environment is not connected."));
301
}
292
- let minted: string | null = null;
293
- const result = await runAttachmentUploadCycle({
302
+ const result = yield* runAttachmentUploadCycle({
303
registry: appAtomRegistry,
304
createUploadUrl: attachmentEnvironment.createUploadUrl,
305
remove: attachmentEnvironment.remove,
@@ -298,7 +307,7 @@ export async function uploadComposerAttachment(
307
upload: attachmentUploadInput(attachment),
308
// Resolve at transfer time; reconnects may change the base URL.
309
resolveUploadUrl: (relativeUrl) => {
301
- requireUploadAccess(environmentId);
310
+ if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return null;
311
const connection = appAtomRegistry.get(
312
environmentSession.preparedConnectionValueAtom(environmentId),
313
);
@@ -306,22 +315,27 @@ export async function uploadComposerAttachment(
315
? null
316
: resolveAssetUrl(connection.value.httpBaseUrl, relativeUrl);
317
},
309
- transport: (url) => ({
310
- done: uploadFileBytes(environmentId, attachment, url, signal, onProgress),
311
- abort: () => {},
312
- }),
313
- onMinted: (attachmentId) => {
314
- if (signal.aborted) return "cancel";
315
- minted = attachmentId;
316
- return "continue";
317
- },
318
+ transport: (url) =>
319
+ Effect.tryPromise({
320
+ try: (signal) => uploadFileBytes(environmentId, attachment, url, signal, onProgress),
321
+ catch: attachmentUploadError,
322
+ }),
323
});
324
if (result.status === "uploaded") return result.attachmentId;
320
- if (minted !== null) await releaseCreatedUploadsQuietly(environmentId, [minted]);
321
- throw result.status === "failed" && result.error !== undefined
322
- ? result.error
323
- : new Error(`Upload failed for '${attachment.name}'.`);
324
-}
325
+ if (result.attachmentId !== null) {
326
+ yield* Effect.promise(() =>
327
+ releaseCreatedUploadsQuietly(environmentId, [result.attachmentId!]),
328
+ );
329
+ }
330
+ return yield* attachmentUploadError(
331
+ result.step === "resolve-url" &&
332
+ !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)
333
+ ? new Error("This connection cannot upload attachments.")
334
+ : result.error instanceof Error
335
+ ? result.error
336
+ : new Error(`Upload failed for '${attachment.name}'.`),
337
+ );
338
+});
339
340
/**
341
* Turns a message's attachments into wire references. `uploadAttachment` is the shared upload
@@ -335,7 +349,7 @@ export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(functi
349
readonly supportsImageUploads?: boolean;
350
readonly uploadAttachment: (
351
attachment: DraftComposerAttachment,
338
- ) => Effect.Effect<string, ComposerAttachmentUploadError>;
352
+ ) => Effect.Effect<string, AttachmentUploadError>;
353
readonly persistUploadedReferences?: (
354
draftAttachments: ReadonlyArray<DraftComposerAttachment>,
355
) => Effect.Effect<"persisted" | "abandon">;
@@ -349,7 +363,7 @@ export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(functi
363
(attachment): attachment is DraftComposerImageAttachment => attachment.type === "image",
364
),
365
),
352
- catch: uploadError,
366
+ catch: attachmentUploadError,
367
});
368
const files = input.attachments.filter((attachment) => attachment.type === "file");
369
if (input.attachments.length === 0 || (files.length === 0 && !input.supportsImageUploads)) {
@@ -362,7 +376,10 @@ export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(functi
376
} satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult;
377
}
378
365
- yield* Effect.try({ try: () => requireUploadAccess(environmentId), catch: uploadError });
379
+ yield* Effect.try({
380
+ try: () => requireUploadAccess(environmentId),
381
+ catch: attachmentUploadError,
382
+ });
383
const uploadedAttachments: UploadedMobileAttachment[] = [];
384
const pendingAttachmentIds: string[] = [];
385
for (const attachment of input.attachments) {
apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts
-246
@@ -1,6 +1,4 @@
1
import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts";
2
-import * as Effect from "effect/Effect";
3
-import * as Fiber from "effect/Fiber";
2
import { describe, expect, it } from "@effect/vitest";
3
import { vi } from "vite-plus/test";
4
import {
@@ -14,9 +12,7 @@ import {
12
composerAttachmentsStillUploading,
13
composerAttachmentUploadKey,
14
composerDraftEnvironmentId,
17
- createComposerAttachmentUploadQueue,
15
type ComposerAttachmentUploadRequest,
19
- type ComposerAttachmentUploadState,
16
} from "./composerAttachmentUploadQueue";
17
18
const environmentId = EnvironmentId.make("environment-1");
@@ -34,248 +30,6 @@ function request(id: string, environment = environmentId): ComposerAttachmentUpl
30
};
31
}
32
37
-describe("composer attachment upload queue", () => {
38
- it("bounds concurrency, deduplicates updates, and drains all attachments", async () => {
39
- const gates = new Map<string, ReturnType<typeof Promise.withResolvers<string>>>();
40
- const fourthStarted = Promise.withResolvers<void>();
41
- const firstThreeStarted = Promise.withResolvers<void>();
42
- let active = 0;
43
- let maximum = 0;
44
- const upload = vi.fn(async (input: ComposerAttachmentUploadRequest) => {
45
- active += 1;
46
- maximum = Math.max(maximum, active);
47
- const gate = Promise.withResolvers<string>();
48
- gates.set(input.attachment.id, gate);
49
- if (gates.size === 3) firstThreeStarted.resolve();
50
- if (gates.size === 4) fourthStarted.resolve();
51
- try {
52
- return await gate.promise;
53
- } finally {
54
- active -= 1;
55
- }
56
- });
57
- const queue = createComposerAttachmentUploadQueue({ upload, onChange: () => {} });
58
- const requests = [request("one"), request("two"), request("three"), request("four")];
59
- queue.sync(requests);
60
- queue.sync(requests);
61
- await firstThreeStarted.promise;
62
- expect(upload).toHaveBeenCalledTimes(3);
63
- gates.get("one")!.resolve("uploaded");
64
- await fourthStarted.promise;
65
- for (const gate of gates.values()) gate.resolve("uploaded");
66
- await queue.settled();
67
- queue.sync(requests);
68
- await queue.settled();
69
- expect(maximum).toBe(3);
70
- expect(upload).toHaveBeenCalledTimes(4);
71
- queue.dispose();
72
- });
73
-
74
- it("cancels on disconnect and resumes from the same local draft on reconnect", async () => {
75
- const started = Promise.withResolvers<void>();
76
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
77
- let signal: AbortSignal | undefined;
78
- const upload = vi.fn(
79
- async (_request: ComposerAttachmentUploadRequest, currentSignal: AbortSignal) => {
80
- signal = currentSignal;
81
- started.resolve();
82
- return new Promise<string>((resolve) =>
83
- currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }),
84
- );
85
- },
86
- );
87
- const queue = createComposerAttachmentUploadQueue({
88
- upload,
89
- onChange: (next) => {
90
- states = next;
91
- },
92
- });
93
- const local = request("offline-draft");
94
- queue.sync([local]);
95
- await started.promise;
96
- queue.sync([]);
97
- await queue.settled();
98
- expect(signal?.aborted).toBe(true);
99
- expect(states).toEqual({});
100
- upload.mockResolvedValueOnce("uploaded");
101
- queue.sync([local]);
102
- await queue.settled();
103
- expect(upload.mock.calls[1]?.[0]).toBe(local);
104
- expect(states[composerAttachmentUploadKey(environmentId, local.attachment.id)]).toEqual({
105
- status: "ready",
106
- });
107
- expect(local.attachment).toMatchObject({ fileUri: "file:///documents/offline-draft.pdf" });
108
- queue.dispose();
109
- });
110
-
111
- it("retries a failed attachment when the worker restores requests after reconnect", async () => {
112
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
113
- const upload = vi
114
- .fn()
115
- .mockRejectedValueOnce(new Error("Disconnected"))
116
- .mockResolvedValue("uploaded");
117
- const queue = createComposerAttachmentUploadQueue({
118
- upload,
119
- onChange: (next) => {
120
- states = next;
121
- },
122
- });
123
- const local = request("failed-upload");
124
- const key = composerAttachmentUploadKey(environmentId, local.attachment.id);
125
- queue.sync([local]);
126
- await queue.settled();
127
- expect(states[key]?.status).toBe("failed");
128
- queue.sync([local]);
129
- await queue.settled();
130
- expect(upload).toHaveBeenCalledTimes(1);
131
- queue.sync([]);
132
- queue.sync([local]);
133
- await queue.settled();
134
- expect(upload).toHaveBeenCalledTimes(2);
135
- expect(states[key]?.status).toBe("ready");
136
- queue.dispose();
137
- });
138
-
139
- it("ignores a late completion after removal or environment switch", async () => {
140
- const gate = Promise.withResolvers<string>();
141
- const started = Promise.withResolvers<void>();
142
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
143
- const upload = vi.fn(async () => {
144
- started.resolve();
145
- return gate.promise;
146
- });
147
- const queue = createComposerAttachmentUploadQueue({
148
- upload,
149
- onChange: (next) => {
150
- states = next;
151
- },
152
- });
153
- queue.sync([request("photo")]);
154
- await started.promise;
155
- upload.mockResolvedValueOnce("uploaded");
156
- const other = EnvironmentId.make("environment-2");
157
- queue.sync([request("photo", other)]);
158
- gate.resolve("uploaded");
159
- await queue.settled();
160
- expect(states).toEqual({ [composerAttachmentUploadKey(other, "photo")]: { status: "ready" } });
161
- queue.sync([]);
162
- expect(states).toEqual({});
163
- queue.dispose();
164
- });
165
-
166
- it("restarts a re-added attachment after its aborted transfer finishes settling", async () => {
167
- const firstStarted = Promise.withResolvers<void>();
168
- const firstSettled = Promise.withResolvers<string>();
169
- const secondStarted = Promise.withResolvers<void>();
170
- const secondSettled = Promise.withResolvers<string>();
171
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
172
- let firstSignal: AbortSignal | undefined;
173
- const upload = vi.fn(async (_request: ComposerAttachmentUploadRequest, signal: AbortSignal) => {
174
- if (!firstSignal) {
175
- firstSignal = signal;
176
- firstStarted.resolve();
177
- return firstSettled.promise;
178
- }
179
- secondStarted.resolve();
180
- return secondSettled.promise;
181
- });
182
- const queue = createComposerAttachmentUploadQueue({
183
- upload,
184
- onChange: (next) => {
185
- states = next;
186
- },
187
- });
188
- const local = request("re-added");
189
- queue.sync([local]);
190
- await firstStarted.promise;
191
- queue.sync([]);
192
- queue.sync([local]);
193
- expect(firstSignal?.aborted).toBe(true);
194
- expect(upload).toHaveBeenCalledOnce();
195
- firstSettled.resolve("aborted");
196
- await secondStarted.promise;
197
- expect(upload).toHaveBeenCalledTimes(2);
198
- secondSettled.resolve("uploaded");
199
- await queue.settled();
200
- expect(states[composerAttachmentUploadKey(environmentId, local.attachment.id)]).toEqual({
201
- status: "ready",
202
- });
203
- queue.dispose();
204
- });
205
-
206
- it("keeps failures stable until retry and reports bounded progress", async () => {
207
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
208
- const progress: number[] = [];
209
- const upload = vi.fn(
210
- async (
211
- _request: ComposerAttachmentUploadRequest,
212
- _signal: AbortSignal,
213
- report: (value: number) => void,
214
- ): Promise<string> => {
215
- report(0.12);
216
- report(0.13);
217
- report(1.1);
218
- throw new Error("Server unavailable");
219
- },
220
- );
221
- const queue = createComposerAttachmentUploadQueue({
222
- upload,
223
- onChange: (next) => {
224
- states = next;
225
- const state = next[composerAttachmentUploadKey(environmentId, "file")];
226
- if (state?.status === "uploading") progress.push(state.progress);
227
- },
228
- });
229
- queue.sync([request("file")]);
230
- await queue.settled();
231
- queue.sync([request("file")]);
232
- expect(upload).toHaveBeenCalledOnce();
233
- expect(states[composerAttachmentUploadKey(environmentId, "file")]).toEqual({
234
- status: "failed",
235
- reason: "Server unavailable",
236
- });
237
- expect(progress).toEqual([0, 0.1, 1]);
238
- upload.mockImplementationOnce(async () => "uploaded");
239
- queue.retry(environmentId, "file");
240
- await queue.settled();
241
- expect(states[composerAttachmentUploadKey(environmentId, "file")]).toEqual({ status: "ready" });
242
- queue.dispose();
243
- });
244
-
245
- it.effect("joins a send to the running upload instead of uploading again", () =>
246
- Effect.gen(function* () {
247
- const gate = Promise.withResolvers<string>();
248
- const started = Promise.withResolvers<void>();
249
- const uploaded = vi.fn();
250
- const upload = vi.fn(async () => {
251
- started.resolve();
252
- return gate.promise;
253
- });
254
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
255
- const queue = createComposerAttachmentUploadQueue({
256
- upload,
257
- onUploaded: uploaded,
258
- onChange: (next) => {
259
- states = next;
260
- },
261
- });
262
- const local = request("video");
263
- queue.sync([local]);
264
- yield* Effect.promise(() => started.promise);
265
- // Sending hands the attachment from the draft to the outbox, which uploads it too.
266
- const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true });
267
- queue.sync([]);
268
- gate.resolve("uploaded-1");
269
- expect(yield* Fiber.join(sent)).toBe("uploaded-1");
270
- expect(upload).toHaveBeenCalledOnce();
271
- expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1");
272
- yield* Effect.promise(() => queue.settled());
273
- expect(states).toEqual({});
274
- queue.dispose();
275
- }),
276
- );
277
-});
278
-
33
describe("draft upload scope and offline submission", () => {
34
it("uploads question drafts in their environment without matching other thread prefixes", () => {
35
const requestId = ApprovalRequestId.make("request:1");
apps/mobile/src/lib/composerAttachmentUploadQueue.ts
+2
-200
@@ -1,14 +1,6 @@
1
import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
2
-import * as Effect from "effect/Effect";
3
-import * as Equal from "effect/Equal";
4
-import * as Exit from "effect/Exit";
5
-import * as Fiber from "effect/Fiber";
6
-import * as Hash from "effect/Hash";
7
-import * as RcMap from "effect/RcMap";
8
-import * as Schema from "effect/Schema";
9
-import * as Scope from "effect/Scope";
10
-import * as Semaphore from "effect/Semaphore";
2
import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
3
+import type { AttachmentUploadStatus } from "@t3tools/client-runtime/state/attachment-uploads";
4
5
import { parseLegacyNewTaskDraftKey } from "../state/new-task-draft-key";
6
import type { DraftComposerAttachment } from "./composerImages";
@@ -18,10 +10,7 @@ export interface ComposerAttachmentUploadRequest {
10
readonly attachment: DraftComposerAttachment;
11
}
12
21
-export type ComposerAttachmentUploadState =
22
- | { readonly status: "uploading"; readonly progress: number }
23
- | { readonly status: "ready" }
24
- | { readonly status: "failed"; readonly reason: string };
13
+export type ComposerAttachmentUploadState = AttachmentUploadStatus;
14
15
export function composerAttachmentUploadKey(
16
environmentId: EnvironmentId,
@@ -113,190 +102,3 @@ export function composerAttachmentsStillUploading(input: {
102
return state?.status !== "ready" && state?.status !== "failed";
103
});
104
}
116
-
117
-/** Equal by environment and attachment id; the request rides along for the upload. */
118
-class UploadKey implements Equal.Equal {
119
- constructor(
120
- readonly id: string,
121
- readonly request: ComposerAttachmentUploadRequest | null,
122
- ) {}
123
- static of(request: ComposerAttachmentUploadRequest): UploadKey {
124
- return new UploadKey(
125
- composerAttachmentUploadKey(request.environmentId, request.attachment.id),
126
- request,
127
- );
128
- }
129
- [Equal.symbol](that: unknown): boolean {
130
- return that instanceof UploadKey && that.id === this.id;
131
- }
132
- [Hash.symbol](): number {
133
- return Hash.string(this.id);
134
- }
135
-}
136
-
137
-/**
138
- * Every upload of a composer attachment, shared by key: the composer holds the attachments it
139
- * shows or has queued, and a send joins the upload already running instead of starting another.
140
- * An upload is interrupted once nothing holds it.
141
- */
142
-export function createComposerAttachmentUploadQueue(options: {
143
- readonly upload: (
144
- request: ComposerAttachmentUploadRequest,
145
- signal: AbortSignal,
146
- onProgress: (progress: number) => void,
147
- ) => Promise<string>;
148
- /** Saves a finished upload's id; the upload is ready once this succeeds. */
149
- readonly onUploaded?: (
150
- request: ComposerAttachmentUploadRequest,
151
- uploadedId: string,
152
- ) => Effect.Effect<void, ComposerAttachmentUploadError>;
153
- readonly onChange: (states: Readonly<Record<string, ComposerAttachmentUploadState>>) => void;
154
-}) {
155
- let states: Readonly<Record<string, ComposerAttachmentUploadState>> = {};
156
- function setState(key: string, state: ComposerAttachmentUploadState | undefined) {
157
- const previous = states[key];
158
- if (
159
- previous === state ||
160
- (previous?.status === "uploading" &&
161
- state?.status === "uploading" &&
162
- previous.progress === state.progress)
163
- )
164
- return;
165
- const next = { ...states };
166
- if (state) next[key] = state;
167
- else delete next[key];
168
- states = next;
169
- options.onChange(states);
170
- }
171
-
172
- const permits = Semaphore.makeUnsafe(3);
173
- const running = new Set<Fiber.Fiber<string, ComposerAttachmentUploadError>>();
174
- // The latest transfer per attachment: a re-added attachment waits for an aborted one to settle.
175
- const transfers = new Map<string, Fiber.Fiber<string, ComposerAttachmentUploadError>>();
176
-
177
- /** The native transfer; interrupting it aborts the request and waits for it to settle. */
178
- const nativeTransfer = (key: UploadKey) =>
179
- Effect.suspend(() => {
180
- const controller = new AbortController();
181
- if (states[key.id]?.status !== "uploading") {
182
- setState(key.id, { status: "uploading", progress: 0 });
183
- }
184
- const done = options.upload(key.request!, controller.signal, (progress) => {
185
- if (controller.signal.aborted) return;
186
- setState(key.id, {
187
- status: "uploading",
188
- progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20,
189
- });
190
- });
191
- return Effect.tryPromise({ try: () => done, catch: uploadError }).pipe(
192
- Effect.onInterrupt(() =>
193
- Effect.sync(() => controller.abort()).pipe(
194
- Effect.andThen(Effect.tryPromise({ try: () => done, catch: uploadError })),
195
- Effect.ignore,
196
- ),
197
- ),
198
- );
199
- });
200
-
201
- const transfer = (key: UploadKey, previous: Fiber.Fiber<string, unknown> | undefined) =>
202
- (previous === undefined ? Effect.void : Fiber.await(previous)).pipe(
203
- Effect.andThen(nativeTransfer(key).pipe(Semaphore.withPermits(permits, 1))),
204
- Effect.tap((uploadedId) => options.onUploaded?.(key.request!, uploadedId) ?? Effect.void),
205
- Effect.tap(() => Effect.sync(() => setState(key.id, { status: "ready" }))),
206
- Effect.tapError((error) =>
207
- Effect.sync(() => setState(key.id, { status: "failed", reason: error.message })),
208
- ),
209
- );
210
-
211
- const scope = Scope.makeUnsafe();
212
- const uploads = Effect.runSync(
213
- Scope.provide(scope)(
214
- RcMap.make({
215
- lookup: Effect.fn("ComposerAttachmentUploads.lookup")(function* (key: UploadKey) {
216
- setState(key.id, { status: "uploading", progress: 0 });
217
- yield* Effect.addFinalizer(() => Effect.sync(() => setState(key.id, undefined)));
218
- const fiber = yield* Effect.forkScoped(transfer(key, transfers.get(key.id)));
219
- transfers.set(key.id, fiber);
220
- running.add(fiber);
221
- fiber.addObserver(() => {
222
- running.delete(fiber);
223
- if (transfers.get(key.id) === fiber) transfers.delete(key.id);
224
- });
225
- return fiber;
226
- }),
227
- }),
228
- ),
229
- );
230
-
231
- const holders = new Map<string, Scope.Closeable>();
232
- let requested: ReadonlyArray<ComposerAttachmentUploadRequest> = [];
233
- let disposed = false;
234
- const release = (id: string) => {
235
- const holder = holders.get(id);
236
- if (!holder) return;
237
- holders.delete(id);
238
- Effect.runFork(Scope.close(holder, Exit.void));
239
- };
240
-
241
- /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */
242
- function sync(requests: ReadonlyArray<ComposerAttachmentUploadRequest>) {
243
- if (disposed) return;
244
- requested = requests;
245
- const keys = new Map(requests.map((request) => [UploadKey.of(request).id, request]));
246
- for (const id of holders.keys()) {
247
- if (!keys.has(id)) release(id);
248
- }
249
- for (const [id, request] of keys) {
250
- if (holders.has(id)) continue;
251
- const holder = Scope.makeUnsafe();
252
- holders.set(id, holder);
253
- Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, UploadKey.of(request))));
254
- }
255
- }
256
-
257
- return {
258
- sync,
259
- /** Uploads the attachment, joining its running upload when there is one. */
260
- upload(
261
- request: ComposerAttachmentUploadRequest,
262
- ): Effect.Effect<string, ComposerAttachmentUploadError> {
263
- return disposed
264
- ? Effect.fail(uploadError(new Error("Upload cancelled.")))
265
- : RcMap.get(uploads, UploadKey.of(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped);
266
- },
267
- retry(environmentId: EnvironmentId, attachmentId: string) {
268
- const id = composerAttachmentUploadKey(environmentId, attachmentId);
269
- if (states[id]?.status !== "failed") return;
270
- Effect.runSync(RcMap.invalidate(uploads, new UploadKey(id, null)));
271
- release(id);
272
- sync(requested);
273
- },
274
- async settled() {
275
- while (running.size > 0) {
276
- await Effect.runPromise(Fiber.awaitAll([...running]));
277
- }
278
- },
279
- dispose() {
280
- disposed = true;
281
- holders.clear();
282
- Effect.runFork(Scope.close(scope, Exit.void));
283
- states = {};
284
- options.onChange(states);
285
- },
286
- };
287
-}
288
-
289
-export function uploadError(cause: unknown): ComposerAttachmentUploadError {
290
- if (isComposerAttachmentUploadError(cause)) return cause;
291
- return new ComposerAttachmentUploadError({
292
- message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.",
293
- cause,
294
- });
295
-}
296
-
297
-export class ComposerAttachmentUploadError extends Schema.TaggedError<ComposerAttachmentUploadError>()(
298
- "ComposerAttachmentUploadError",
299
- { message: Schema.String, cause: Schema.Defect() },
300
-) {}
301
-
302
-const isComposerAttachmentUploadError = Schema.is(ComposerAttachmentUploadError);
apps/mobile/src/state/attachments.test.ts
+29
-21
@@ -6,7 +6,10 @@ import {
6
import { runAttachmentUploadCycle } from "@t3tools/client-runtime/state/attachments";
7
import * as Cause from "effect/Cause";
8
import { AsyncResult, AtomRegistry } from "effect/reactivity";
9
-import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
+import { describe, expect, it } from "@effect/vitest";
10
+import * as Effect from "effect/Effect";
11
+import * as Fiber from "effect/Fiber";
12
+import { afterEach, beforeEach, vi } from "vite-plus/test";
13
14
const state = vi.hoisted(() => ({
15
grantedEnvironments: new Set<string>(),
@@ -76,24 +79,29 @@ describe("attachment mutation grants", () => {
79
expect(state.remove).toHaveBeenCalledOnce();
80
});
81
79
- it("blocks the upload cycle's asynchronous cleanup after grant revocation", async () => {
80
- state.grantedEnvironments.add("secondary");
81
- const transport = vi.fn();
82
- const result = await runAttachmentUploadCycle({
83
- registry,
84
- ...attachmentEnvironment,
85
- environmentId,
86
- upload: { name: "image.png", mimeType: "image/png", sizeBytes: 1 },
87
- resolveUploadUrl: () => "https://example.test/upload",
88
- transport,
89
- onMinted: () => {
90
- state.grantedEnvironments.delete("secondary");
91
- return "cancel";
92
- },
93
- });
94
- expect(result.status).toBe("cancelled");
95
- expect(state.create).toHaveBeenCalledOnce();
96
- expect(state.remove).not.toHaveBeenCalled();
97
- expect(transport).not.toHaveBeenCalled();
98
- });
82
+ it.effect("blocks the upload cycle's cleanup after grant revocation", () =>
83
+ Effect.gen(function* () {
84
+ state.grantedEnvironments.add("secondary");
85
+ const transferStarted = Promise.withResolvers<void>();
86
+ const cycle = yield* Effect.forkChild(
87
+ runAttachmentUploadCycle({
88
+ registry,
89
+ ...attachmentEnvironment,
90
+ environmentId,
91
+ upload: { name: "image.png", mimeType: "image/png", sizeBytes: 1 },
92
+ resolveUploadUrl: () => "https://example.test/upload",
93
+ transport: () =>
94
+ Effect.callback<void>(() => {
95
+ state.grantedEnvironments.delete("secondary");
96
+ transferStarted.resolve();
97
+ }),
98
+ }),
99
+ );
100
+ yield* Effect.promise(() => transferStarted.promise);
101
+ // Interrupting deletes the fresh mint, which the revoked grant must refuse.
102
+ yield* Fiber.interrupt(cycle);
103
+ expect(state.create).toHaveBeenCalledOnce();
104
+ expect(state.remove).not.toHaveBeenCalled();
105
+ }),
106
+ );
107
});
apps/mobile/src/state/composer-attachment-uploads.test.ts
+21
-1
@@ -143,7 +143,9 @@ vi.mock("@t3tools/client-runtime/state/runtime", async (importOriginal) => ({
143
) =>
144
native.server.get(query.input.resource.attachmentId)
145
? { _tag: "Success", value: {} }
146
- : { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } },
146
+ : (await import("effect/reactivity")).AsyncResult.failure(
147
+ (await import("effect/Cause")).fail({ _tag: "AssetAttachmentNotFoundError" }),
148
+ ),
149
runAtomCommand: async (
150
_registry: unknown,
151
command: unknown,
@@ -324,6 +326,24 @@ it.effect.each(["decode", "write"] as const)(
326
}),
327
);
328
329
+it.effect("uploads again at send time when the server swept a finished upload", () =>
330
+ Effect.gen(function* () {
331
+ const queue = worker();
332
+ yield* Effect.promise(() => queue.settled());
333
+ expect(uploadState()).toEqual({ status: "ready" });
334
+ // The server sweeps pending uploads nothing claimed, here while the draft sat unsent.
335
+ native.server.delete("pending-1");
336
+
337
+ const message = queuedMessage();
338
+ yield* Effect.promise(() => enqueueThreadOutboxMessage(message));
339
+ const prepared = yield* prepareQueuedMessageAttachments(message);
340
+ expect(prepared.status).toBe("ready");
341
+ if (prepared.status !== "ready") throw new Error("Outbox preparation abandoned");
342
+ expect(prepared.prepared.pendingAttachmentIds).toEqual(["pending-2"]);
343
+ expect(native.transfer).toHaveBeenCalledTimes(2);
344
+ }),
345
+);
346
+
347
it("keeps an upload running when its message is sent mid-upload and records it on the message", async () => {
348
const transferStarted = Promise.withResolvers<void>();
349
const finishTransfer = Promise.withResolvers<{ status: number }>();
apps/mobile/src/state/composer-attachment-uploads.ts
+51
-22
@@ -2,6 +2,11 @@ import { useAtomValue } from "@effect/atom-react";
2
import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts";
3
import * as Effect from "effect/Effect";
4
import * as Schema from "effect/Schema";
5
+import {
6
+ attachmentUploadError,
7
+ createAttachmentUploadRegistry,
8
+} from "@t3tools/client-runtime/state/attachment-uploads";
9
+import { verifyPersistedAttachmentUpload } from "@t3tools/client-runtime/state/attachments";
10
import { Atom } from "effect/reactivity";
11
12
import { uploadComposerAttachment } from "../lib/attachmentUpload";
@@ -11,14 +16,13 @@ import {
16
type DraftComposerAttachment,
17
} from "../lib/composerImages";
18
import {
14
- uploadError,
19
composerAttachmentUploadKey,
20
composerDraftEnvironmentId,
21
canUploadComposerAttachment,
18
- createComposerAttachmentUploadQueue,
22
type ComposerAttachmentUploadState,
23
} from "../lib/composerAttachmentUploadQueue";
24
import { appAtomRegistry } from "./atom-registry";
25
+import { assetEnvironment } from "./assets";
26
import { environmentServerConfigsAtom } from "./server";
27
import {
28
flattenQueuedThreadMessages,
@@ -52,7 +56,7 @@ export const composerAttachmentUploadsAtom = Atom.make<
56
const uploadStateAtom = Atom.family((key: string) =>
57
Atom.map(composerAttachmentUploadsAtom, (states) => states[key]),
58
);
55
-let uploadQueue: ReturnType<typeof createComposerAttachmentUploadQueue> | null = null;
59
+let uploadQueue: ReturnType<typeof createComposerUploadRegistry> | null = null;
60
61
export function useComposerAttachmentUploadState(
62
environmentId: EnvironmentId | undefined,
@@ -65,7 +69,7 @@ export function useComposerAttachmentUploadState(
69
70
export function retryComposerAttachmentUpload(environmentId: EnvironmentId, attachmentId: string) {
71
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return;
68
- uploadQueue?.retry(environmentId, attachmentId);
72
+ uploadQueue?.retry(composerAttachmentUploadKey(environmentId, attachmentId));
73
}
74
75
type ComposerAttachmentUploadRequest = {
@@ -162,7 +166,7 @@ const recordQueuedAttachmentUpload = Effect.fn("ComposerAttachments.recordQueued
166
? null
167
: withUploadedAttachment(current, uploaded),
168
),
165
- catch: uploadError,
169
+ catch: attachmentUploadError,
170
}),
171
{ concurrency: "unbounded", discard: true },
172
);
@@ -226,7 +230,7 @@ const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUplo
230
yield* recordQueuedAttachmentUpload(environmentId, uploaded);
231
if (!draftsChanged) return;
232
// A failed draft save only costs a re-upload after restart; the outbox saves its own references.
229
- yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: uploadError }).pipe(
233
+ yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: attachmentUploadError }).pipe(
234
Effect.catchIf(
235
(error) => isComposerDraftPersistenceError(error.cause),
236
(error) =>
@@ -238,29 +242,54 @@ const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUplo
242
);
243
});
244
241
-function composerAttachmentUploads() {
242
- uploadQueue ??= createComposerAttachmentUploadQueue({
245
+const createComposerUploadRegistry = () =>
246
+ createAttachmentUploadRegistry<ComposerAttachmentUploadRequest>({
247
+ key: (request) => composerAttachmentUploadKey(request.environmentId, request.attachment.id),
248
+ environmentId: (request) => request.environmentId,
249
+ concurrencyPerEnvironment: 3,
250
onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states),
244
- upload: ({ environmentId, attachment }, signal, onProgress) => {
245
- const release = isFileBackedComposerAttachment(attachment)
246
- ? retainComposerAttachmentFileForPreview(attachment)
247
- : undefined;
248
- return uploadComposerAttachment(environmentId, attachment, signal, onProgress).finally(() =>
249
- release?.(),
250
- );
251
- },
251
+ // The file stays on disk while its upload reads it, even if its draft lets go meanwhile.
252
+ upload: ({ environmentId, attachment }, reportProgress) =>
253
+ Effect.acquireUseRelease(
254
+ Effect.sync(() =>
255
+ isFileBackedComposerAttachment(attachment)
256
+ ? retainComposerAttachmentFileForPreview(attachment)
257
+ : undefined,
258
+ ),
259
+ () => uploadComposerAttachment(environmentId, attachment, reportProgress),
260
+ (release) => Effect.sync(() => release?.()),
261
+ ),
262
onUploaded: recordComposerAttachmentUpload,
263
});
264
+
265
+function composerAttachmentUploads() {
266
+ uploadQueue ??= createComposerUploadRegistry();
267
return uploadQueue;
268
}
269
270
/** Every send uploads through here, so it joins the composer's upload of the same attachment. */
258
-export function uploadComposerAttachmentShared(
259
- environmentId: EnvironmentId,
260
- attachment: DraftComposerAttachment,
261
-) {
262
- return composerAttachmentUploads().upload({ environmentId, attachment });
263
-}
271
+export const uploadComposerAttachmentShared = Effect.fn("ComposerAttachments.uploadForSend")(
272
+ function* (environmentId: EnvironmentId, attachment: DraftComposerAttachment) {
273
+ const uploads = composerAttachmentUploads();
274
+ const request = { environmentId, attachment };
275
+ const key = composerAttachmentUploadKey(environmentId, attachment.id);
276
+ // An upload that finished earlier can outlive the server's pending-upload sweep.
277
+ const finishedEarlier = uploads.status(key)?.status === "ready";
278
+ const uploadedId = yield* uploads.upload(request);
279
+ if (!finishedEarlier) return uploadedId;
280
+ const verification = yield* verifyPersistedAttachmentUpload({
281
+ registry: appAtomRegistry,
282
+ createAssetUrl: assetEnvironment.createUrl,
283
+ environmentId,
284
+ attachmentId: uploadedId,
285
+ });
286
+ if (verification.status === "verified") return uploadedId;
287
+ if (verification.status === "failed") return yield* attachmentUploadError(verification.error);
288
+ // Starting over verifies the saved id again and uploads the bytes when it is gone.
289
+ uploads.restart(key);
290
+ return yield* uploads.upload(request);
291
+ },
292
+);
293
294
/**
295
* Uploads draft and queued attachments to their environment as soon as it is connected and
apps/mobile/src/state/thread-outbox-model.ts
+1
-1
@@ -289,7 +289,7 @@ export function shouldRetryThreadOutboxDelivery(error: unknown): boolean {
289
case "EnvironmentNotRegisteredError":
290
return true;
291
// A shared upload's failure decides by what made the upload fail.
292
- case "ComposerAttachmentUploadError":
292
+ case "AttachmentUploadError":
293
return "cause" in error && shouldRetryThreadOutboxDelivery(error.cause);
294
default:
295
break;
apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts
+4
-2
@@ -7,7 +7,7 @@ import {
7
ProviderInstanceId,
8
ThreadId,
9
} from "@t3tools/contracts";
10
-import { uploadError } from "../lib/composerAttachmentUploadQueue";
10
+import { attachmentUploadError } from "@t3tools/client-runtime/state/attachment-uploads";
11
import * as Effect from "effect/Effect";
12
import * as Cause from "effect/Cause";
13
import { AsyncResult, type Atom } from "effect/reactivity";
@@ -497,7 +497,9 @@ describe("queued task operation access", () => {
497
Effect.suspend(() => {
498
setOperateGrant("secondary", false);
499
return uploadFails
500
- ? Effect.fail(uploadError(new Error("This connection cannot upload attachments.")))
500
+ ? Effect.fail(
501
+ attachmentUploadError(new Error("This connection cannot upload attachments.")),
502
+ )
503
: Effect.succeed({
504
status: "ready",
505
attachments: [],
apps/web/src/lib/attachmentUploadQueue.ts
+171
-219
@@ -7,6 +7,10 @@ import {
7
} from "@t3tools/contracts";
8
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
9
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
10
+import {
11
+ attachmentUploadError,
12
+ createAttachmentUploadRegistry,
13
+} from "@t3tools/client-runtime/state/attachment-uploads";
14
import {
15
deletePendingAttachmentUpload,
16
runAttachmentUploadCycle,
@@ -14,6 +18,8 @@ import {
18
type PersistedAttachmentVerification,
19
} from "@t3tools/client-runtime/state/attachments";
20
import { create } from "zustand";
21
+import * as Effect from "effect/Effect";
22
+import * as Fiber from "effect/Fiber";
23
import * as Option from "effect/Option";
24
import { AsyncResult } from "effect/reactivity";
25
@@ -50,19 +56,19 @@ interface UploadJob {
56
readonly previous?: ReadyAttachmentUpload;
57
/** Draft-owned; delete only through releasePersistedAttachmentUpload, never queue cancel/retry. */
58
readonly persistedAttachmentId?: string;
53
- readonly settled: Promise<void>;
54
- resolveSettled: () => void;
55
- attachmentId: string | null;
56
- cancelled: boolean;
57
- abort: (() => void) | null;
58
- stopWatchingConnection: () => void;
59
}
60
61
type DraftFileOwner = Pick<UploadJob, "image" | "environmentId" | "draftTarget">;
62
63
-const jobsByImageId = new Map<string, UploadJob>();
64
-const queue: UploadJob[] = [];
65
-const activeUploadsByEnvironment = new Map<EnvironmentId, number>();
63
+/** The upload each image holds; a failed one stays held for its retry. */
64
+interface HeldUpload {
65
+ readonly job: UploadJob;
66
+ readonly release: () => void;
67
+ readonly stopWatchingConnection: () => void;
68
+}
69
+
70
+const heldUploads = new Map<string, HeldUpload>();
71
+const jobKey = (job: UploadJob) => `${job.environmentId}:${job.image.id}`;
72
73
function setUploadState(imageId: string, upload: AttachmentUploadState): void {
74
useAttachmentUploadStore.setState((state) => ({
@@ -137,10 +143,11 @@ function uploadBytes(input: {
143
readonly url: string;
144
readonly file: File;
145
readonly mimeType: string;
146
+ readonly signal: AbortSignal;
147
readonly onProgress: (progress: number) => void;
141
-}): { readonly done: Promise<void>; readonly abort: () => void } {
148
+}): Promise<void> {
149
const xhr = new XMLHttpRequest();
143
- const done = new Promise<void>((resolve, reject) => {
150
+ return new Promise<void>((resolve, reject) => {
151
xhr.open("POST", input.url, true);
152
xhr.timeout = UPLOAD_TIMEOUT_MS;
153
xhr.setRequestHeader("Content-Type", input.mimeType);
@@ -159,32 +166,38 @@ function uploadBytes(input: {
166
xhr.addEventListener("error", () => reject(new Error("Upload failed")));
167
xhr.addEventListener("timeout", () => reject(new Error("Upload timed out")));
168
xhr.addEventListener("abort", () => reject(new Error("Upload cancelled")));
169
+ input.signal.addEventListener("abort", () => xhr.abort(), { once: true });
170
xhr.send(input.file);
171
});
164
-
165
- return { done, abort: () => xhr.abort() };
172
}
173
168
-async function runUpload(job: UploadJob): Promise<void> {
169
- if (!readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)) {
174
+const canOperate = (environmentId: EnvironmentId) =>
175
+ readEnvironmentScope(environmentId, AuthOrchestrationOperateScope);
176
+
177
+/** Records the failure the composer shows, then fails the upload with it. */
178
+const failUpload = (job: UploadJob, reason: string, attachmentId?: string) =>
179
+ Effect.sync(() =>
180
setUploadState(job.image.id, {
181
status: "failed",
182
environmentId: job.environmentId,
173
- reason: "This connection cannot upload attachments.",
183
+ reason,
184
+ ...(attachmentId ? { attachmentId } : {}),
185
...(job.previous ? { previous: job.previous } : {}),
175
- });
176
- return;
186
+ }),
187
+ ).pipe(Effect.andThen(Effect.fail(attachmentUploadError(new Error(reason)))));
188
+
189
+/** Uploads one composer attachment, writing the state its composer shows as it goes. */
190
+const uploadJob = Effect.fn("WebAttachmentUploads.upload")(function* (job: UploadJob) {
191
+ if (!canOperate(job.environmentId)) {
192
+ return yield* failUpload(job, "This connection cannot upload attachments.");
193
}
194
if (job.persistedAttachmentId) {
179
- const verification = await verifyPersistedAttachmentUpload({
195
+ const verification = yield* verifyPersistedAttachmentUpload({
196
registry: appAtomRegistry,
197
createAssetUrl: assetEnvironment.createUrl,
198
environmentId: job.environmentId,
199
attachmentId: job.persistedAttachmentId,
200
});
185
- if (job.cancelled) {
186
- return;
187
- }
201
if (verification.status === "verified") {
202
setUploadState(job.image.id, {
203
status: "ready",
@@ -192,7 +205,7 @@ async function runUpload(job: UploadJob): Promise<void> {
205
attachmentId: job.persistedAttachmentId,
206
});
207
stampDraftFileUpload(job, job.persistedAttachmentId);
195
- return;
208
+ return job.persistedAttachmentId;
209
}
210
if (verification.status === "missing" && !job.image.file && job.image.type === "file") {
211
const draftTarget = resolveCurrentFileDraftTarget(job);
@@ -207,22 +220,19 @@ async function runUpload(job: UploadJob): Promise<void> {
220
job.persistedAttachmentId,
221
)
222
) {
223
+ // The draft now shows the file as missing, so the composer has no upload to show.
224
clearUploadState(job.image.id);
211
- return;
225
+ return yield* attachmentUploadError(new Error("Uploaded file expired."));
226
}
227
}
228
if (verification.status === "failed" || !job.image.file) {
229
// Failed-state ids get deleted on retry/release; exclude persisted ids after transient verification failure.
216
- setUploadState(job.image.id, {
217
- status: "failed",
218
- environmentId: job.environmentId,
219
- reason:
220
- verification.status === "missing"
221
- ? "Uploaded file expired. Remove it and attach it again."
222
- : "Uploaded file could not be verified. Retry when the server reconnects.",
223
- ...(job.previous ? { previous: job.previous } : {}),
224
- });
225
- return;
230
+ return yield* failUpload(
231
+ job,
232
+ verification.status === "missing"
233
+ ? "Uploaded file expired. Remove it and attach it again."
234
+ : "Uploaded file could not be verified. Retry when the server reconnects.",
235
+ );
236
}
237
}
238
@@ -232,37 +242,15 @@ async function runUpload(job: UploadJob): Promise<void> {
242
: PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES.find(
243
(supportedMimeType) => supportedMimeType === job.image.mimeType.toLowerCase(),
244
);
235
- if (!mimeType) {
236
- setUploadState(job.image.id, {
237
- status: "failed",
238
- environmentId: job.environmentId,
239
- reason: "Unsupported image type",
240
- ...(job.previous ? { previous: job.previous } : {}),
241
- });
242
- return;
243
- }
245
+ if (!mimeType) return yield* failUpload(job, "Unsupported image type");
246
const file = job.image.file;
245
- if (!file) {
246
- setUploadState(job.image.id, {
247
- status: "failed",
248
- environmentId: job.environmentId,
249
- reason: "Original file is no longer available",
250
- ...(job.previous ? { previous: job.previous } : {}),
251
- });
252
- return;
247
+ if (!file) return yield* failUpload(job, "Original file is no longer available");
248
+ if (!canOperate(job.environmentId)) {
249
+ return yield* failUpload(job, "This connection cannot upload attachments.");
250
}
251
252
let lastStep = -1;
256
- if (!readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)) {
257
- setUploadState(job.image.id, {
258
- status: "failed",
259
- environmentId: job.environmentId,
260
- reason: "This connection cannot upload attachments.",
261
- ...(job.previous ? { previous: job.previous } : {}),
262
- });
263
- return;
264
- }
265
- const result = await runAttachmentUploadCycle({
253
+ const result = yield* runAttachmentUploadCycle({
254
registry: appAtomRegistry,
255
createUploadUrl: attachmentEnvironment.createUploadUrl,
256
remove: attachmentEnvironment.remove,
@@ -274,49 +262,33 @@ async function runUpload(job: UploadJob): Promise<void> {
262
sizeBytes: file.size,
263
},
264
resolveUploadUrl: (relativeUrl) => {
277
- if (
278
- job.cancelled ||
279
- !readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)
280
- ) {
281
- return null;
282
- }
265
+ if (!canOperate(job.environmentId)) return null;
266
const connection = readPreparedConnection(job.environmentId);
267
return connection ? resolveAssetUrl(connection.httpBaseUrl, relativeUrl) : null;
268
},
269
transport: (url) =>
287
- uploadBytes({
288
- url,
289
- file,
290
- mimeType,
291
- onProgress: (progress) => {
292
- const step = Math.floor(progress * 20);
293
- if (step === lastStep || job.cancelled) {
294
- return;
295
- }
296
- lastStep = step;
297
- setUploadState(job.image.id, {
298
- status: "uploading",
299
- environmentId: job.environmentId,
300
- progress,
301
- ...(job.previous ? { previous: job.previous } : {}),
302
- });
303
- },
270
+ Effect.tryPromise({
271
+ try: (signal) =>
272
+ uploadBytes({
273
+ url,
274
+ file,
275
+ mimeType,
276
+ signal,
277
+ onProgress: (progress) => {
278
+ const step = Math.floor(progress * 20);
279
+ if (step === lastStep || signal.aborted) return;
280
+ lastStep = step;
281
+ setUploadState(job.image.id, {
282
+ status: "uploading",
283
+ environmentId: job.environmentId,
284
+ progress,
285
+ ...(job.previous ? { previous: job.previous } : {}),
286
+ });
287
+ },
288
+ }),
289
+ catch: attachmentUploadError,
290
}),
305
- onMinted: (attachmentId) => {
306
- if (job.cancelled && readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)) {
307
- return "cancel";
308
- }
309
- job.attachmentId = attachmentId;
310
- return "continue";
311
- },
312
- onTransferStart: (abort) => {
313
- job.abort = abort;
314
- },
291
});
316
- job.abort = null;
317
- if (result.status === "cancelled" || job.cancelled) {
318
- return;
319
- }
292
if (result.status === "uploaded") {
293
setUploadState(job.image.id, {
294
status: "ready",
@@ -327,69 +299,42 @@ async function runUpload(job: UploadJob): Promise<void> {
299
if (job.previous) {
300
deletePendingUpload(job.previous.environmentId, job.previous.attachmentId);
301
}
330
- return;
331
- }
332
- setUploadState(job.image.id, {
333
- status: "failed",
334
- environmentId: job.environmentId,
335
- reason:
336
- result.step === "mint"
337
- ? "Upload could not start"
338
- : result.step === "resolve-url"
339
- ? readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)
340
- ? "Not connected"
341
- : "This connection cannot upload attachments."
342
- : result.error instanceof Error
343
- ? result.error.message
344
- : "Upload failed",
345
- ...(result.attachmentId ? { attachmentId: result.attachmentId } : {}),
346
- ...(job.previous ? { previous: job.previous } : {}),
347
- });
348
-}
349
-
350
-function pumpUploads(): void {
351
- for (let index = 0; index < queue.length;) {
352
- const job = queue[index]!;
353
- const active = activeUploadsByEnvironment.get(job.environmentId) ?? 0;
354
- if (active >= MAX_UPLOADS_PER_ENVIRONMENT) {
355
- index += 1;
356
- continue;
357
- }
358
-
359
- queue.splice(index, 1);
360
- if (job.cancelled) {
361
- continue;
362
- }
363
- activeUploadsByEnvironment.set(job.environmentId, active + 1);
364
- void runUpload(job)
365
- .catch(() => {
366
- if (!job.cancelled) {
367
- setUploadState(job.image.id, {
368
- status: "failed",
369
- environmentId: job.environmentId,
370
- reason: "Upload failed",
371
- ...(job.previous ? { previous: job.previous } : {}),
372
- });
373
- }
374
- })
375
- .finally(() => {
376
- if (
377
- jobsByImageId.get(job.image.id) === job &&
378
- readAttachmentUpload(job.image.id)?.status !== "failed"
379
- ) {
380
- job.stopWatchingConnection();
381
- jobsByImageId.delete(job.image.id);
382
- }
383
- const remaining = (activeUploadsByEnvironment.get(job.environmentId) ?? 1) - 1;
384
- if (remaining > 0) {
385
- activeUploadsByEnvironment.set(job.environmentId, remaining);
386
- } else {
387
- activeUploadsByEnvironment.delete(job.environmentId);
388
- }
389
- job.resolveSettled();
390
- pumpUploads();
391
- });
302
+ return result.attachmentId;
303
}
304
+ return yield* failUpload(
305
+ job,
306
+ result.step === "mint"
307
+ ? "Upload could not start"
308
+ : result.step === "resolve-url"
309
+ ? canOperate(job.environmentId)
310
+ ? "Not connected"
311
+ : "This connection cannot upload attachments."
312
+ : result.error instanceof Error
313
+ ? result.error.message
314
+ : "Upload failed",
315
+ result.attachmentId ?? undefined,
316
+ );
317
+});
318
+
319
+const uploads = createAttachmentUploadRegistry<UploadJob>({
320
+ key: jobKey,
321
+ environmentId: (job) => job.environmentId,
322
+ concurrencyPerEnvironment: MAX_UPLOADS_PER_ENVIRONMENT,
323
+ upload: (job) =>
324
+ uploadJob(job).pipe(
325
+ Effect.catchDefect((defect) =>
326
+ failUpload(job, "Upload failed").pipe(
327
+ Effect.ensuring(Effect.logWarning("Composer attachment upload threw", defect)),
328
+ ),
329
+ ),
330
+ ),
331
+});
332
+
333
+/** Waits for an image's held upload to finish, without keeping it running. */
334
+function awaitHeldUpload(imageId: string): Effect.Effect<void> {
335
+ const held = heldUploads.get(imageId);
336
+ const fiber = held === undefined ? undefined : uploads.current(jobKey(held.job));
337
+ return fiber === undefined ? Effect.void : Effect.asVoid(Fiber.await(fiber));
338
}
339
340
export function startAttachmentUpload(input: {
@@ -398,9 +343,9 @@ export function startAttachmentUpload(input: {
343
readonly draftTarget?: ComposerThreadTarget;
344
readonly force?: boolean;
345
}): void {
401
- if (!readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)) return;
402
- const existingJob = jobsByImageId.get(input.image.id);
403
- if (existingJob?.environmentId === input.environmentId) {
346
+ if (!canOperate(input.environmentId)) return;
347
+ const existingHold = heldUploads.get(input.image.id);
348
+ if (existingHold?.job.environmentId === input.environmentId) {
349
return;
350
}
351
@@ -433,14 +378,10 @@ export function startAttachmentUpload(input: {
378
return;
379
}
380
436
- if (existingJob) {
381
+ if (existingHold) {
382
cancelAttachmentUpload(input.image.id);
383
}
384
const previous = existing?.status === "ready" ? existing : existing?.previous;
440
- let resolveSettled: () => void = () => {};
441
- const settled = new Promise<void>((resolve) => {
442
- resolveSettled = resolve;
443
- });
385
const job: UploadJob = {
386
image: input.image,
387
environmentId: input.environmentId,
@@ -452,65 +393,68 @@ export function startAttachmentUpload(input: {
393
input.image.uploadedAttachmentId
394
? { persistedAttachmentId: input.image.uploadedAttachmentId }
395
: {}),
455
- settled,
456
- resolveSettled,
457
- attachmentId: null,
458
- cancelled: false,
459
- abort: null,
460
- stopWatchingConnection: () => {},
396
};
397
463
- jobsByImageId.set(input.image.id, job);
398
+ setUploadState(input.image.id, {
399
+ status: "uploading",
400
+ environmentId: input.environmentId,
401
+ progress: 0,
402
+ ...(previous ? { previous } : {}),
403
+ });
404
const connectionAtom = environmentCatalog.stateAtom(job.environmentId);
405
const isConnected = () =>
406
Option.exists(
407
AsyncResult.value(appAtomRegistry.get(connectionAtom)),
408
(state) => state.phase === "connected",
409
);
410
+ const ownsImage = () => heldUploads.get(input.image.id)?.job === job;
411
let wasConnected = isConnected();
471
- job.stopWatchingConnection = appAtomRegistry.subscribe(connectionAtom, () => {
412
+ const stopWatchingConnection = appAtomRegistry.subscribe(connectionAtom, () => {
413
const connected = isConnected();
414
const reconnected = connected && !wasConnected;
415
wasConnected = connected;
416
if (!reconnected) return;
417
// HTTP failures can follow reconnection; await the attempt and retry only if this job still owns the file.
477
- void job.settled.then(() => {
478
- if (
479
- jobsByImageId.get(job.image.id) === job &&
480
- readAttachmentUpload(job.image.id)?.status === "failed" &&
481
- isConnected()
482
- ) {
483
- retryAttachmentUpload(input);
484
- }
485
- });
418
+ Effect.runFork(
419
+ awaitHeldUpload(input.image.id).pipe(
420
+ Effect.andThen(
421
+ Effect.sync(() => {
422
+ if (
423
+ ownsImage() &&
424
+ readAttachmentUpload(input.image.id)?.status === "failed" &&
425
+ isConnected()
426
+ ) {
427
+ retryAttachmentUpload(input);
428
+ }
429
+ }),
430
+ ),
431
+ ),
432
+ );
433
});
487
- queue.push(job);
488
- setUploadState(input.image.id, {
489
- status: "uploading",
490
- environmentId: input.environmentId,
491
- progress: 0,
492
- ...(previous ? { previous } : {}),
434
+ heldUploads.set(input.image.id, {
435
+ job,
436
+ release: uploads.hold(job),
437
+ stopWatchingConnection,
438
+ });
439
+ // A finished upload lets go of its image; a failed one stays held for its retry.
440
+ uploads.current(jobKey(job))?.addObserver((exit) => {
441
+ if (exit._tag !== "Success" || !ownsImage()) return;
442
+ const held = heldUploads.get(input.image.id)!;
443
+ heldUploads.delete(input.image.id);
444
+ held.stopWatchingConnection();
445
+ held.release();
446
});
494
- pumpUploads();
447
}
448
449
function cancelAttachmentUpload(imageId: string): void {
498
- const job = jobsByImageId.get(imageId);
499
- if (!job) {
450
+ const held = heldUploads.get(imageId);
451
+ if (!held) {
452
return;
453
}
502
- job.cancelled = true;
503
- job.stopWatchingConnection();
504
- jobsByImageId.delete(imageId);
505
- const queuedIndex = queue.indexOf(job);
506
- if (queuedIndex !== -1) {
507
- queue.splice(queuedIndex, 1);
508
- }
509
- job.abort?.();
510
- if (job.attachmentId) {
511
- deletePendingUpload(job.environmentId, job.attachmentId);
512
- }
513
- job.resolveSettled();
454
+ heldUploads.delete(imageId);
455
+ held.stopWatchingConnection();
456
+ // Releasing interrupts a running upload, which aborts its transfer and deletes its mint.
457
+ held.release();
458
}
459
460
export function releaseAttachmentUpload(imageId: string): void {
@@ -543,10 +487,10 @@ export function releasePersistedAttachmentUpload(input: {
487
releaseAttachmentUpload(input.id);
488
return;
489
}
546
- const job = jobsByImageId.get(input.id);
490
+ const held = heldUploads.get(input.id);
491
if (
548
- job?.environmentId === input.environmentId &&
549
- job.persistedAttachmentId === input.attachmentId
492
+ held?.job.environmentId === input.environmentId &&
493
+ held.job.persistedAttachmentId === input.attachmentId
494
) {
495
releaseAttachmentUpload(input.id);
496
}
@@ -558,9 +502,12 @@ export function retryAttachmentUpload(input: {
502
readonly image: ComposerImageAttachment | ComposerFileAttachment;
503
readonly draftTarget?: ComposerThreadTarget;
504
}): void {
561
- if (!readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)) return;
505
+ if (!canOperate(input.environmentId)) return;
506
const previous = readAttachmentUpload(input.image.id);
507
+ const held = heldUploads.get(input.image.id);
508
cancelAttachmentUpload(input.image.id);
509
+ // The failed upload stays cached until dropped, and the retry must start a fresh one.
510
+ if (held) uploads.invalidate(jobKey(held.job));
511
if (previous?.status === "failed" && previous.attachmentId) {
512
deletePendingUpload(previous.environmentId, previous.attachmentId);
513
}
@@ -577,16 +524,21 @@ export function verifyStashedAttachmentUpload(input: {
524
readonly environmentId: EnvironmentId;
525
readonly attachmentId: string;
526
}): Promise<PersistedAttachmentVerification> {
580
- return verifyPersistedAttachmentUpload({
581
- registry: appAtomRegistry,
582
- createAssetUrl: assetEnvironment.createUrl,
583
- environmentId: input.environmentId,
584
- attachmentId: input.attachmentId,
585
- });
527
+ return Effect.runPromise(
528
+ verifyPersistedAttachmentUpload({
529
+ registry: appAtomRegistry,
530
+ createAssetUrl: assetEnvironment.createUrl,
531
+ environmentId: input.environmentId,
532
+ attachmentId: input.attachmentId,
533
+ }),
534
+ );
535
}
536
588
-export async function awaitAttachmentUploads(imageIds: ReadonlyArray<string>): Promise<void> {
589
- await Promise.all(imageIds.map((imageId) => jobsByImageId.get(imageId)?.settled));
537
+/** Resolves once these images' uploads finish, fail or are cancelled. */
538
+export function awaitAttachmentUploads(imageIds: ReadonlyArray<string>): Promise<void> {
539
+ return Effect.runPromise(
540
+ Effect.forEach(imageIds, awaitHeldUpload, { concurrency: "unbounded", discard: true }),
541
+ );
542
}
543
544
/** Revalidate ready uploads at send time; a long-lived draft can outlive server cleanup. */
@@ -685,7 +637,7 @@ export function releaseDraftAttachment(
637
environmentId: attachment.uploadEnvironmentId,
638
attachmentId: attachment.uploadedAttachmentId,
639
});
688
- if (jobsByImageId.has(attachment.id) || readAttachmentUpload(attachment.id)) {
640
+ if (heldUploads.has(attachment.id) || readAttachmentUpload(attachment.id)) {
641
releaseAttachmentUpload(attachment.id);
642
}
643
return;
packages/client-runtime/package.json
+4
@@ -175,6 +175,10 @@
175
"types": "./src/state/attachments.ts",
176
"default": "./src/state/attachments.ts"
177
},
178
+ "./state/attachment-uploads": {
179
+ "types": "./src/state/attachmentUploads.ts",
180
+ "default": "./src/state/attachmentUploads.ts"
181
+ },
182
"./text-paste": {
183
"types": "./src/textPaste.ts",
184
"default": "./src/textPaste.ts"
packages/client-runtime/src/state/attachmentUploads.test.ts
new
+301
@@ -0,0 +1,301 @@
1
+import { EnvironmentId } from "@t3tools/contracts";
2
+import { describe, expect, it } from "@effect/vitest";
3
+import * as Effect from "effect/Effect";
4
+import * as Fiber from "effect/Fiber";
5
+import { vi } from "vite-plus/test";
6
+
7
+import {
8
+ attachmentUploadError,
9
+ createAttachmentUploadRegistry,
10
+ type AttachmentUploadError,
11
+ type AttachmentUploadStatus,
12
+} from "./attachmentUploads.ts";
13
+
14
+interface UploadRequest {
15
+ readonly environmentId: EnvironmentId;
16
+ readonly attachment: { readonly id: string; readonly fileUri: string };
17
+}
18
+
19
+const environmentId = EnvironmentId.make("environment-1");
20
+const uploadKey = (environment: EnvironmentId, id: string) => `${environment}:${id}`;
21
+
22
+function request(id: string, environment = environmentId): UploadRequest {
23
+ return { environmentId: environment, attachment: { id, fileUri: `file:///documents/${id}.pdf` } };
24
+}
25
+
26
+/** The registry over a promise-based transfer, as each client wires its own. */
27
+function createQueue(options: {
28
+ readonly upload: (
29
+ request: UploadRequest,
30
+ signal: AbortSignal,
31
+ onProgress: (progress: number) => void,
32
+ ) => Promise<string>;
33
+ readonly onUploaded?: (
34
+ request: UploadRequest,
35
+ uploadedId: string,
36
+ ) => Effect.Effect<void, AttachmentUploadError>;
37
+ readonly onChange: (statuses: Readonly<Record<string, AttachmentUploadStatus>>) => void;
38
+ readonly concurrencyPerEnvironment?: number;
39
+}) {
40
+ return createAttachmentUploadRegistry<UploadRequest>({
41
+ key: (candidate) => uploadKey(candidate.environmentId, candidate.attachment.id),
42
+ environmentId: (candidate) => candidate.environmentId,
43
+ concurrencyPerEnvironment: options.concurrencyPerEnvironment ?? 3,
44
+ upload: (candidate, report) =>
45
+ Effect.tryPromise({
46
+ try: (signal) => options.upload(candidate, signal, report),
47
+ catch: attachmentUploadError,
48
+ }),
49
+ ...(options.onUploaded ? { onUploaded: options.onUploaded } : {}),
50
+ onChange: options.onChange,
51
+ });
52
+}
53
+
54
+describe("attachment upload registry", () => {
55
+ it("bounds concurrency, deduplicates updates, and drains all attachments", async () => {
56
+ const gates = new Map<string, ReturnType<typeof Promise.withResolvers<string>>>();
57
+ const fourthStarted = Promise.withResolvers<void>();
58
+ const firstThreeStarted = Promise.withResolvers<void>();
59
+ let active = 0;
60
+ let maximum = 0;
61
+ const upload = vi.fn(async (input: UploadRequest) => {
62
+ active += 1;
63
+ maximum = Math.max(maximum, active);
64
+ const gate = Promise.withResolvers<string>();
65
+ gates.set(input.attachment.id, gate);
66
+ if (gates.size === 3) firstThreeStarted.resolve();
67
+ if (gates.size === 4) fourthStarted.resolve();
68
+ try {
69
+ return await gate.promise;
70
+ } finally {
71
+ active -= 1;
72
+ }
73
+ });
74
+ const queue = createQueue({ upload, onChange: () => {} });
75
+ const requests = [request("one"), request("two"), request("three"), request("four")];
76
+ queue.sync(requests);
77
+ queue.sync(requests);
78
+ await firstThreeStarted.promise;
79
+ expect(upload).toHaveBeenCalledTimes(3);
80
+ gates.get("one")!.resolve("uploaded");
81
+ await fourthStarted.promise;
82
+ for (const gate of gates.values()) gate.resolve("uploaded");
83
+ await queue.settled();
84
+ queue.sync(requests);
85
+ await queue.settled();
86
+ expect(maximum).toBe(3);
87
+ expect(upload).toHaveBeenCalledTimes(4);
88
+ queue.dispose();
89
+ });
90
+
91
+ it("cancels on disconnect and resumes from the same local draft on reconnect", async () => {
92
+ const started = Promise.withResolvers<void>();
93
+ let states: Readonly<Record<string, AttachmentUploadStatus>> = {};
94
+ let signal: AbortSignal | undefined;
95
+ const upload = vi.fn(async (_request: UploadRequest, currentSignal: AbortSignal) => {
96
+ signal = currentSignal;
97
+ started.resolve();
98
+ return new Promise<string>((resolve) =>
99
+ currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }),
100
+ );
101
+ });
102
+ const queue = createQueue({
103
+ upload,
104
+ onChange: (next) => {
105
+ states = next;
106
+ },
107
+ });
108
+ const local = request("offline-draft");
109
+ queue.sync([local]);
110
+ await started.promise;
111
+ queue.sync([]);
112
+ await queue.settled();
113
+ expect(signal?.aborted).toBe(true);
114
+ expect(states).toEqual({});
115
+ upload.mockResolvedValueOnce("uploaded");
116
+ queue.sync([local]);
117
+ await queue.settled();
118
+ expect(upload.mock.calls[1]?.[0]).toBe(local);
119
+ expect(states[uploadKey(environmentId, local.attachment.id)]).toEqual({
120
+ status: "ready",
121
+ });
122
+ expect(local.attachment).toMatchObject({ fileUri: "file:///documents/offline-draft.pdf" });
123
+ queue.dispose();
124
+ });
125
+
126
+ it("retries a failed attachment when the worker restores requests after reconnect", async () => {
127
+ let states: Readonly<Record<string, AttachmentUploadStatus>> = {};
128
+ const upload = vi
129
+ .fn()
130
+ .mockRejectedValueOnce(new Error("Disconnected"))
131
+ .mockResolvedValue("uploaded");
132
+ const queue = createQueue({
133
+ upload,
134
+ onChange: (next) => {
135
+ states = next;
136
+ },
137
+ });
138
+ const local = request("failed-upload");
139
+ const key = uploadKey(environmentId, local.attachment.id);
140
+ queue.sync([local]);
141
+ await queue.settled();
142
+ expect(states[key]?.status).toBe("failed");
143
+ queue.sync([local]);
144
+ await queue.settled();
145
+ expect(upload).toHaveBeenCalledTimes(1);
146
+ queue.sync([]);
147
+ queue.sync([local]);
148
+ await queue.settled();
149
+ expect(upload).toHaveBeenCalledTimes(2);
150
+ expect(states[key]?.status).toBe("ready");
151
+ queue.dispose();
152
+ });
153
+
154
+ it("ignores a late completion after removal or environment switch", async () => {
155
+ const gate = Promise.withResolvers<string>();
156
+ const started = Promise.withResolvers<void>();
157
+ let states: Readonly<Record<string, AttachmentUploadStatus>> = {};
158
+ const upload = vi.fn(async () => {
159
+ started.resolve();
160
+ return gate.promise;
161
+ });
162
+ const queue = createQueue({
163
+ upload,
164
+ onChange: (next) => {
165
+ states = next;
166
+ },
167
+ });
168
+ queue.sync([request("photo")]);
169
+ await started.promise;
170
+ upload.mockResolvedValueOnce("uploaded");
171
+ const other = EnvironmentId.make("environment-2");
172
+ queue.sync([request("photo", other)]);
173
+ gate.resolve("uploaded");
174
+ await queue.settled();
175
+ expect(states).toEqual({ [uploadKey(other, "photo")]: { status: "ready" } });
176
+ queue.sync([]);
177
+ expect(states).toEqual({});
178
+ queue.dispose();
179
+ });
180
+
181
+ it("keeps failures stable until retry and reports bounded progress", async () => {
182
+ let states: Readonly<Record<string, AttachmentUploadStatus>> = {};
183
+ const progress: number[] = [];
184
+ const upload = vi.fn(
185
+ async (
186
+ _request: UploadRequest,
187
+ _signal: AbortSignal,
188
+ report: (value: number) => void,
189
+ ): Promise<string> => {
190
+ report(0.12);
191
+ report(0.13);
192
+ report(1.1);
193
+ throw new Error("Server unavailable");
194
+ },
195
+ );
196
+ const queue = createQueue({
197
+ upload,
198
+ onChange: (next) => {
199
+ states = next;
200
+ const state = next[uploadKey(environmentId, "file")];
201
+ if (state?.status === "uploading") progress.push(state.progress);
202
+ },
203
+ });
204
+ queue.sync([request("file")]);
205
+ await queue.settled();
206
+ queue.sync([request("file")]);
207
+ expect(upload).toHaveBeenCalledOnce();
208
+ expect(states[uploadKey(environmentId, "file")]).toEqual({
209
+ status: "failed",
210
+ reason: "Server unavailable",
211
+ });
212
+ expect(progress).toEqual([0, 0.1, 1]);
213
+ upload.mockImplementationOnce(async () => "uploaded");
214
+ queue.retry(uploadKey(environmentId, "file"));
215
+ await queue.settled();
216
+ expect(states[uploadKey(environmentId, "file")]).toEqual({ status: "ready" });
217
+ queue.dispose();
218
+ });
219
+
220
+ it.effect("joins a send to the running upload instead of uploading again", () =>
221
+ Effect.gen(function* () {
222
+ const gate = Promise.withResolvers<string>();
223
+ const started = Promise.withResolvers<void>();
224
+ const uploaded = vi.fn();
225
+ const upload = vi.fn(async () => {
226
+ started.resolve();
227
+ return gate.promise;
228
+ });
229
+ let states: Readonly<Record<string, AttachmentUploadStatus>> = {};
230
+ const queue = createQueue({
231
+ upload,
232
+ onUploaded: uploaded,
233
+ onChange: (next) => {
234
+ states = next;
235
+ },
236
+ });
237
+ const local = request("video");
238
+ queue.sync([local]);
239
+ yield* Effect.promise(() => started.promise);
240
+ // Sending hands the attachment from the draft to the outbox, which uploads it too.
241
+ const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true });
242
+ queue.sync([]);
243
+ gate.resolve("uploaded-1");
244
+ expect(yield* Fiber.join(sent)).toBe("uploaded-1");
245
+ expect(upload).toHaveBeenCalledOnce();
246
+ expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1");
247
+ yield* Effect.promise(() => queue.settled());
248
+ expect(states).toEqual({});
249
+ queue.dispose();
250
+ }),
251
+ );
252
+ it.effect("restarts a failed upload when a send joins it instead of rejoining the failure", () =>
253
+ Effect.gen(function* () {
254
+ const upload = vi
255
+ .fn()
256
+ .mockRejectedValueOnce(new Error("Server unavailable"))
257
+ .mockResolvedValue("uploaded-2");
258
+ const queue = createQueue({ upload, onChange: () => {} });
259
+ const local = request("photo");
260
+ queue.sync([local]);
261
+ yield* Effect.promise(() => queue.settled());
262
+ expect(queue.status(uploadKey(environmentId, "photo"))?.status).toBe("failed");
263
+
264
+ expect(yield* queue.upload(local)).toBe("uploaded-2");
265
+ expect(upload).toHaveBeenCalledTimes(2);
266
+ queue.dispose();
267
+ }),
268
+ );
269
+
270
+ it.effect("keeps a replacement upload's status when the upload it replaced is released", () =>
271
+ Effect.gen(function* () {
272
+ const replacement = Promise.withResolvers<string>();
273
+ const upload = vi
274
+ .fn()
275
+ .mockRejectedValueOnce(new Error("Server unavailable"))
276
+ .mockImplementationOnce(() => replacement.promise);
277
+ let statuses: Readonly<Record<string, AttachmentUploadStatus>> = {};
278
+ const queue = createQueue({
279
+ upload,
280
+ onChange: (next) => {
281
+ statuses = next;
282
+ },
283
+ });
284
+ const local = request("photo");
285
+ const releaseFailed = queue.hold(local);
286
+ yield* Effect.promise(() => queue.settled());
287
+ queue.invalidate(uploadKey(environmentId, "photo"));
288
+ const releaseReplacement = queue.hold(local);
289
+ releaseFailed();
290
+ expect(statuses[uploadKey(environmentId, "photo")]).toEqual({
291
+ status: "uploading",
292
+ progress: 0,
293
+ });
294
+ replacement.resolve("uploaded-2");
295
+ yield* Effect.promise(() => queue.settled());
296
+ expect(statuses[uploadKey(environmentId, "photo")]).toEqual({ status: "ready" });
297
+ releaseReplacement();
298
+ queue.dispose();
299
+ }),
300
+ );
301
+});
packages/client-runtime/src/state/attachmentUploads.ts
new
+240
@@ -0,0 +1,240 @@
1
+import type { EnvironmentId } from "@t3tools/contracts";
2
+import * as Effect from "effect/Effect";
3
+import * as Equal from "effect/Equal";
4
+import * as Exit from "effect/Exit";
5
+import * as Fiber from "effect/Fiber";
6
+import * as Hash from "effect/Hash";
7
+import * as RcMap from "effect/RcMap";
8
+import * as Schema from "effect/Schema";
9
+import * as Scope from "effect/Scope";
10
+import * as Semaphore from "effect/Semaphore";
11
+
12
+export class AttachmentUploadError extends Schema.TaggedError<AttachmentUploadError>()(
13
+ "AttachmentUploadError",
14
+ { message: Schema.String, cause: Schema.Defect() },
15
+) {}
16
+
17
+const isAttachmentUploadError = Schema.is(AttachmentUploadError);
18
+
19
+export function attachmentUploadError(cause: unknown): AttachmentUploadError {
20
+ if (isAttachmentUploadError(cause)) return cause;
21
+ return new AttachmentUploadError({
22
+ message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.",
23
+ cause,
24
+ });
25
+}
26
+
27
+export type AttachmentUploadStatus =
28
+ | { readonly status: "uploading"; readonly progress: number }
29
+ | { readonly status: "ready" }
30
+ | { readonly status: "failed"; readonly reason: string };
31
+
32
+/** Equal by key; the request rides along for the upload. */
33
+class UploadKey<Request> implements Equal.Equal {
34
+ readonly id: string;
35
+ readonly request: Request | null;
36
+ constructor(id: string, request: Request | null) {
37
+ this.id = id;
38
+ this.request = request;
39
+ }
40
+ [Equal.symbol](that: unknown): boolean {
41
+ return that instanceof UploadKey && that.id === this.id;
42
+ }
43
+ [Hash.symbol](): number {
44
+ return Hash.string(this.id);
45
+ }
46
+}
47
+
48
+/**
49
+ * Every upload of an attachment, shared by key. Holders keep an upload running, and anyone who
50
+ * needs its result joins the running upload instead of starting another. An upload is
51
+ * interrupted once nothing holds it.
52
+ */
53
+export function createAttachmentUploadRegistry<Request>(options: {
54
+ readonly key: (request: Request) => string;
55
+ readonly environmentId: (request: Request) => EnvironmentId;
56
+ /** Uploads running at once against one environment; the rest wait their turn. */
57
+ readonly concurrencyPerEnvironment: number;
58
+ /** Uploads one attachment and returns its uploaded id; interrupting it cancels the transfer. */
59
+ readonly upload: (
60
+ request: Request,
61
+ reportProgress: (progress: number) => void,
62
+ ) => Effect.Effect<string, AttachmentUploadError>;
63
+ /** Saves a finished upload's id; the upload is ready once this succeeds. */
64
+ readonly onUploaded?: (
65
+ request: Request,
66
+ uploadedId: string,
67
+ ) => Effect.Effect<void, AttachmentUploadError>;
68
+ readonly onChange?: (statuses: Readonly<Record<string, AttachmentUploadStatus>>) => void;
69
+}) {
70
+ let statuses: Readonly<Record<string, AttachmentUploadStatus>> = {};
71
+ function setStatus(key: string, status: AttachmentUploadStatus | undefined) {
72
+ const previous = statuses[key];
73
+ if (
74
+ previous === status ||
75
+ (previous?.status === "uploading" &&
76
+ status?.status === "uploading" &&
77
+ previous.progress === status.progress)
78
+ )
79
+ return;
80
+ const next = { ...statuses };
81
+ if (status) next[key] = status;
82
+ else delete next[key];
83
+ statuses = next;
84
+ options.onChange?.(statuses);
85
+ }
86
+
87
+ const permits = new Map<EnvironmentId, Semaphore.Semaphore>();
88
+ const permitsFor = (environmentId: EnvironmentId) => {
89
+ let semaphore = permits.get(environmentId);
90
+ if (semaphore === undefined) {
91
+ semaphore = Semaphore.makeUnsafe(options.concurrencyPerEnvironment);
92
+ permits.set(environmentId, semaphore);
93
+ }
94
+ return semaphore;
95
+ };
96
+ const running = new Map<string, Fiber.Fiber<string, AttachmentUploadError>>();
97
+ // The entry that owns each key's status: an invalidated entry finishing or closing late must
98
+ // not overwrite or clear the status of the entry that replaced it.
99
+ const owners = new Map<string, object>();
100
+ const setOwnStatus = (owner: object, key: string, status: AttachmentUploadStatus | undefined) => {
101
+ if (owners.get(key) === owner) setStatus(key, status);
102
+ };
103
+
104
+ const transfer = (key: UploadKey<Request>, owner: object) => {
105
+ const request = key.request!;
106
+ return Effect.suspend(() => {
107
+ if (statuses[key.id]?.status !== "uploading") {
108
+ setOwnStatus(owner, key.id, { status: "uploading", progress: 0 });
109
+ }
110
+ return options.upload(request, (progress) =>
111
+ setOwnStatus(owner, key.id, {
112
+ status: "uploading",
113
+ progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20,
114
+ }),
115
+ );
116
+ }).pipe(
117
+ Semaphore.withPermits(permitsFor(options.environmentId(request)), 1),
118
+ Effect.tap((uploadedId) => options.onUploaded?.(request, uploadedId) ?? Effect.void),
119
+ Effect.tap(() => Effect.sync(() => setOwnStatus(owner, key.id, { status: "ready" }))),
120
+ Effect.tapError((error) =>
121
+ Effect.sync(() => setOwnStatus(owner, key.id, { status: "failed", reason: error.message })),
122
+ ),
123
+ );
124
+ };
125
+
126
+ const scope = Scope.makeUnsafe();
127
+ const uploads = Effect.runSync(
128
+ Scope.provide(scope)(
129
+ RcMap.make({
130
+ lookup: Effect.fn("AttachmentUploads.lookup")(function* (key: UploadKey<Request>) {
131
+ const owner = {};
132
+ owners.set(key.id, owner);
133
+ setStatus(key.id, { status: "uploading", progress: 0 });
134
+ yield* Effect.addFinalizer(() =>
135
+ Effect.sync(() => {
136
+ setOwnStatus(owner, key.id, undefined);
137
+ if (owners.get(key.id) === owner) owners.delete(key.id);
138
+ }),
139
+ );
140
+ // Starts in the holder's call, so an upload begins as soon as something holds it.
141
+ const fiber = yield* Effect.forkScoped(transfer(key, owner), {
142
+ startImmediately: true,
143
+ });
144
+ running.set(key.id, fiber);
145
+ fiber.addObserver(() => {
146
+ if (running.get(key.id) === fiber) running.delete(key.id);
147
+ });
148
+ return fiber;
149
+ }),
150
+ }),
151
+ ),
152
+ );
153
+ const keyOf = (request: Request) => new UploadKey(options.key(request), request);
154
+
155
+ let disposed = false;
156
+ /** Keeps the request's upload running until the returned release is called. */
157
+ function hold(request: Request): () => void {
158
+ if (disposed) return () => {};
159
+ const holder = Scope.makeUnsafe();
160
+ Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, keyOf(request))));
161
+ let released = false;
162
+ return () => {
163
+ if (released) return;
164
+ released = true;
165
+ Effect.runFork(Scope.close(holder, Exit.void));
166
+ };
167
+ }
168
+
169
+ const synced = new Map<string, () => void>();
170
+ let requested: ReadonlyArray<Request> = [];
171
+ /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */
172
+ function sync(requests: ReadonlyArray<Request>) {
173
+ if (disposed) return;
174
+ requested = requests;
175
+ const keys = new Map(requests.map((request) => [options.key(request), request]));
176
+ for (const [id, release] of synced) {
177
+ if (keys.has(id)) continue;
178
+ synced.delete(id);
179
+ release();
180
+ }
181
+ for (const [id, request] of keys) {
182
+ if (!synced.has(id)) synced.set(id, hold(request));
183
+ }
184
+ }
185
+
186
+ /** Drops a failed upload so the next holder starts it again. */
187
+ function invalidate(key: string) {
188
+ Effect.runSync(RcMap.invalidate(uploads, new UploadKey<Request>(key, null)));
189
+ }
190
+
191
+ /** Drops the key's upload and starts it again for whatever `sync` holds. */
192
+ function restart(key: string) {
193
+ invalidate(key);
194
+ synced.get(key)?.();
195
+ synced.delete(key);
196
+ sync(requested);
197
+ }
198
+
199
+ return {
200
+ hold,
201
+ sync,
202
+ invalidate,
203
+ restart,
204
+ /**
205
+ * Uploads the request, joining its running or finished upload. A failed one starts again,
206
+ * so a retrying send never just rejoins the failure.
207
+ */
208
+ upload(request: Request): Effect.Effect<string, AttachmentUploadError> {
209
+ return Effect.suspend(() => {
210
+ if (disposed) return Effect.fail(attachmentUploadError(new Error("Upload cancelled.")));
211
+ const key = options.key(request);
212
+ if (statuses[key]?.status === "failed") restart(key);
213
+ return RcMap.get(uploads, keyOf(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped);
214
+ });
215
+ },
216
+ /** Starts a failed upload that `sync` holds again. */
217
+ retry(key: string) {
218
+ if (statuses[key]?.status === "failed") restart(key);
219
+ },
220
+ /** The key's running upload, to await it without holding it. */
221
+ current(key: string): Fiber.Fiber<string, AttachmentUploadError> | undefined {
222
+ return running.get(key);
223
+ },
224
+ status(key: string): AttachmentUploadStatus | undefined {
225
+ return statuses[key];
226
+ },
227
+ async settled() {
228
+ while (running.size > 0) {
229
+ await Effect.runPromise(Fiber.awaitAll([...running.values()]));
230
+ }
231
+ },
232
+ dispose() {
233
+ disposed = true;
234
+ synced.clear();
235
+ Effect.runFork(Scope.close(scope, Exit.void));
236
+ statuses = {};
237
+ options.onChange?.(statuses);
238
+ },
239
+ };
240
+}
packages/client-runtime/src/state/attachments.test.ts
+72
-63
@@ -6,6 +6,7 @@ import {
6
type AttachmentDeleteInput,
7
} from "@t3tools/contracts";
8
import * as Effect from "effect/Effect";
9
+import * as Fiber from "effect/Fiber";
10
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
11
12
import type { AtomCommand } from "./runtime.ts";
@@ -59,79 +60,87 @@ const uploadInput: AttachmentCreateUploadUrlInput = {
60
};
61
62
describe("runAttachmentUploadCycle", () => {
62
- it("deletes the fresh mint when the caller cancels at onMinted", async () => {
63
- removeCalls.length = 0;
64
- const result = await runAttachmentUploadCycle({
65
- registry,
66
- createUploadUrl: makeCreateUploadUrl("pending-cancelled"),
67
- remove,
68
- environmentId,
69
- upload: uploadInput,
70
- resolveUploadUrl: () => "https://environment.test/upload",
71
- transport: () => {
72
- throw new Error("transport must not run after cancel");
73
- },
74
- onMinted: () => "cancel",
75
- });
76
-
77
- expect(result).toEqual({ status: "cancelled", attachmentId: "pending-cancelled" });
78
- expect(removeCalls).toEqual(["pending-cancelled"]);
79
- });
80
-
81
- it("keeps the minted id on transfer failure so the caller can retry or release", async () => {
82
- removeCalls.length = 0;
83
- const result = await runAttachmentUploadCycle({
84
- registry,
85
- createUploadUrl: makeCreateUploadUrl("pending-failed"),
86
- remove,
87
- environmentId,
88
- upload: uploadInput,
89
- resolveUploadUrl: () => "https://environment.test/upload",
90
- transport: () => ({
91
- done: Promise.reject(new Error("Upload rejected (413)")),
92
- abort: () => {},
93
- }),
94
- });
63
+ it.effect("aborts the transfer and deletes the fresh mint when interrupted", () =>
64
+ Effect.gen(function* () {
65
+ removeCalls.length = 0;
66
+ const transferStarted = Promise.withResolvers<void>();
67
+ let aborted = false;
68
+ const cycle = yield* Effect.forkChild(
69
+ runAttachmentUploadCycle({
70
+ registry,
71
+ createUploadUrl: makeCreateUploadUrl("pending-cancelled"),
72
+ remove,
73
+ environmentId,
74
+ upload: uploadInput,
75
+ resolveUploadUrl: () => "https://environment.test/upload",
76
+ transport: () =>
77
+ Effect.callback<void>(() => {
78
+ transferStarted.resolve();
79
+ return Effect.sync(() => {
80
+ aborted = true;
81
+ });
82
+ }),
83
+ }),
84
+ );
85
+ yield* Effect.promise(() => transferStarted.promise);
86
+ yield* Fiber.interrupt(cycle);
87
+
88
+ expect(aborted).toBe(true);
89
+ expect(removeCalls).toEqual(["pending-cancelled"]);
90
+ }),
91
+ );
92
+
93
+ it.effect("keeps the minted id on transfer failure so the caller can retry or release", () =>
94
+ Effect.gen(function* () {
95
+ removeCalls.length = 0;
96
+ const result = yield* runAttachmentUploadCycle({
97
+ registry,
98
+ createUploadUrl: makeCreateUploadUrl("pending-failed"),
99
+ remove,
100
+ environmentId,
101
+ upload: uploadInput,
102
+ resolveUploadUrl: () => "https://environment.test/upload",
103
+ transport: () => Effect.fail({ _tag: "UploadRejected", status: 413 } as const),
104
+ });
105
96
- expect(result).toMatchObject({
97
- status: "failed",
98
- step: "transfer",
99
- attachmentId: "pending-failed",
100
- });
101
- expect(removeCalls).toEqual([]);
102
- });
106
+ expect(result).toMatchObject({
107
+ status: "failed",
108
+ step: "transfer",
109
+ attachmentId: "pending-failed",
110
+ });
111
+ expect(removeCalls).toEqual([]);
112
+ }),
113
+ );
114
});
115
116
describe("verifyPersistedAttachmentUpload", () => {
106
- it("hits the server on every verification instead of reusing a cached failure", async () => {
107
- let lookups = 0;
108
- const assetUrlAtom = Atom.make(
109
- Effect.promise(() => Promise.resolve()).pipe(
110
- Effect.flatMap(() => {
111
- lookups += 1;
112
- return lookups === 1
113
- ? Effect.fail({ _tag: "TransportError" } as const)
114
- : Effect.succeed({ url: "/api/assets/pending-1" });
115
- }),
116
- ),
117
- ).pipe(Atom.swr({ staleTime: 60_000 }), Atom.keepAlive);
118
- const liveRegistry = AtomRegistry.make();
119
-
120
- const verify = () =>
121
- verifyPersistedAttachmentUpload({
117
+ it.effect("hits the server on every verification instead of reusing a cached failure", () =>
118
+ Effect.gen(function* () {
119
+ let lookups = 0;
120
+ const assetUrlAtom = Atom.make(
121
+ Effect.promise(() => Promise.resolve()).pipe(
122
+ Effect.flatMap(() => {
123
+ lookups += 1;
124
+ return lookups === 1
125
+ ? Effect.fail({ _tag: "TransportError" } as const)
126
+ : Effect.succeed({ url: "/api/assets/pending-1" });
127
+ }),
128
+ ),
129
+ ).pipe(Atom.swr({ staleTime: 60_000 }), Atom.keepAlive);
130
+ const liveRegistry = AtomRegistry.make();
131
+
132
+ const verify = verifyPersistedAttachmentUpload({
133
registry: liveRegistry,
134
createAssetUrl: () => assetUrlAtom,
135
environmentId,
136
attachmentId: "pending-1",
137
});
138
128
- const first = await verify();
129
- expect(first).toMatchObject({ status: "failed" });
130
-
131
- const second = await verify();
132
- expect(second).toEqual({ status: "verified" });
133
- expect(lookups).toBe(2);
134
- });
139
+ expect(yield* verify).toMatchObject({ status: "failed" });
140
+ expect(yield* verify).toEqual({ status: "verified" });
141
+ expect(lookups).toBe(2);
142
+ }),
143
+ );
144
});
145
146
describe("formatAttachmentSize", () => {
packages/client-runtime/src/state/attachments.ts
+86
-76
@@ -6,6 +6,7 @@ import {
6
type AttachmentDeleteInput,
7
type EnvironmentId,
8
} from "@t3tools/contracts";
9
+import * as Effect from "effect/Effect";
10
import type { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
11
12
import type { EnvironmentRegistry } from "../connection/registry.ts";
@@ -51,7 +52,7 @@ export type PersistedAttachmentVerification =
52
| { readonly status: "missing" }
53
| { readonly status: "failed"; readonly error: unknown };
54
54
-export async function verifyPersistedAttachmentUpload<A, E>(input: {
55
+export function verifyPersistedAttachmentUpload<A, E>(input: {
56
readonly registry: AtomRegistry.AtomRegistry;
57
readonly createAssetUrl: (query: {
58
readonly environmentId: EnvironmentId;
@@ -61,25 +62,29 @@ export async function verifyPersistedAttachmentUpload<A, E>(input: {
62
}) => Atom.Atom<AsyncResult.AsyncResult<A, E>>;
63
readonly environmentId: EnvironmentId;
64
readonly attachmentId: string;
64
-}): Promise<PersistedAttachmentVerification> {
65
- const result = await executeAtomQuery(
66
- input.registry,
67
- input.createAssetUrl({
68
- environmentId: input.environmentId,
69
- input: { resource: { _tag: "attachment", attachmentId: input.attachmentId } },
65
+}): Effect.Effect<PersistedAttachmentVerification> {
66
+ return Effect.promise(() =>
67
+ executeAtomQuery(
68
+ input.registry,
69
+ input.createAssetUrl({
70
+ environmentId: input.environmentId,
71
+ input: { resource: { _tag: "attachment", attachmentId: input.attachmentId } },
72
+ }),
73
+ // `refresh` forces a server round trip: the asset URL query atom caches
74
+ // results (SWR), so a retry right after a transient failure would
75
+ // otherwise re-observe the cached failure and never ask the server.
76
+ { reportFailure: false, reportDefect: false, refresh: true },
77
+ ),
78
+ ).pipe(
79
+ Effect.map((result): PersistedAttachmentVerification => {
80
+ if (result._tag === "Success") return { status: "verified" };
81
+ const error = squashAtomCommandFailure(result);
82
+ return isAssetAttachmentNotFoundFailure(error)
83
+ ? { status: "missing" }
84
+ : { status: "failed", error };
85
}),
71
- // `refresh` forces a server round trip: the asset URL query atom caches
72
- // results (SWR), so a retry right after a transient failure would
73
- // otherwise re-observe the cached failure and never ask the server.
74
- { reportFailure: false, reportDefect: false, refresh: true },
86
+ Effect.withSpan("Attachments.verifyPersistedUpload"),
87
);
76
- if (result._tag === "Success") {
77
- return { status: "verified" };
78
- }
79
- const error = squashAtomCommandFailure(result);
80
- return isAssetAttachmentNotFoundFailure(error)
81
- ? { status: "missing" }
82
- : { status: "failed", error };
88
}
89
90
type AttachmentCreateUploadUrlCommand<E> = AtomCommand<
@@ -108,14 +113,8 @@ export function deletePendingAttachmentUpload<E>(input: {
113
);
114
}
115
111
-export interface AttachmentByteUpload {
112
- readonly done: Promise<void>;
113
- readonly abort: () => void;
114
-}
115
-
116
export type AttachmentUploadCycleResult =
117
| { readonly status: "uploaded"; readonly attachmentId: string }
118
- | { readonly status: "cancelled"; readonly attachmentId: string | null }
118
| {
119
readonly status: "failed";
120
readonly step: "mint" | "resolve-url" | "transfer";
@@ -124,65 +123,76 @@ export type AttachmentUploadCycleResult =
123
};
124
125
/**
127
- * The cycle never deletes the minted pending upload on failure: callers keep the returned
128
- * `attachmentId` and decide between retry and release. The one exception is `onMinted`
129
- * returning `"cancel"`, where the caller already abandoned the upload and the fresh mint is
130
- * deleted before returning.
126
+ * Mints an upload slot and sends the bytes. A failure keeps the minted id in the result so the
127
+ * caller decides between retry and release. Interrupting the cycle aborts the transfer and
128
+ * deletes the fresh mint, including when the interruption arrives while minting.
129
*/
132
-export async function runAttachmentUploadCycle<E, RE>(input: {
130
+export function runAttachmentUploadCycle<E, RE, TE>(input: {
131
readonly registry: AtomRegistry.AtomRegistry;
132
readonly createUploadUrl: AttachmentCreateUploadUrlCommand<E>;
133
readonly remove: AttachmentRemoveCommand<RE>;
134
readonly environmentId: EnvironmentId;
135
readonly upload: AttachmentCreateUploadUrlInput;
136
readonly resolveUploadUrl: (relativeUrl: string) => string | null;
139
- readonly transport: (url: string) => AttachmentByteUpload;
140
- readonly onMinted?: (attachmentId: string) => "continue" | "cancel";
141
- readonly onTransferStart?: (abort: () => void) => void;
142
-}): Promise<AttachmentUploadCycleResult> {
143
- const minted = await runAtomCommand(
144
- input.registry,
145
- input.createUploadUrl,
146
- { environmentId: input.environmentId, input: input.upload },
147
- { reportFailure: false },
148
- );
149
- if (minted._tag !== "Success") {
150
- return {
151
- status: "failed",
152
- step: "mint",
153
- attachmentId: null,
154
- error: squashAtomCommandFailure(minted),
155
- };
156
- }
157
- const attachmentId = minted.value.attachmentId;
158
- if (input.onMinted?.(attachmentId) === "cancel") {
159
- deletePendingAttachmentUpload({
160
- registry: input.registry,
161
- remove: input.remove,
162
- environmentId: input.environmentId,
163
- attachmentId,
164
- });
165
- return { status: "cancelled", attachmentId };
166
- }
167
-
168
- const url = input.resolveUploadUrl(minted.value.relativeUrl);
169
- if (!url) {
170
- return {
171
- status: "failed",
172
- step: "resolve-url",
173
- attachmentId,
174
- error: new Error("The environment is not connected."),
175
- };
176
- }
177
-
178
- const transfer = input.transport(url);
179
- input.onTransferStart?.(transfer.abort);
180
- try {
181
- await transfer.done;
182
- } catch (error) {
183
- return { status: "failed", step: "transfer", attachmentId, error };
184
- }
185
- return { status: "uploaded", attachmentId };
137
+ /** Sends the bytes; interrupting it aborts the transfer. */
138
+ readonly transport: (url: string) => Effect.Effect<void, TE>;
139
+}): Effect.Effect<AttachmentUploadCycleResult> {
140
+ return Effect.uninterruptibleMask((restore) =>
141
+ Effect.gen(function* () {
142
+ const minted = yield* Effect.promise(() =>
143
+ runAtomCommand(
144
+ input.registry,
145
+ input.createUploadUrl,
146
+ { environmentId: input.environmentId, input: input.upload },
147
+ { reportFailure: false },
148
+ ),
149
+ );
150
+ if (minted._tag !== "Success") {
151
+ return {
152
+ status: "failed",
153
+ step: "mint",
154
+ attachmentId: null,
155
+ error: squashAtomCommandFailure(minted),
156
+ } as const;
157
+ }
158
+ const attachmentId = minted.value.attachmentId;
159
+ return yield* restore(
160
+ Effect.suspend(() => {
161
+ const url = input.resolveUploadUrl(minted.value.relativeUrl);
162
+ if (!url) {
163
+ return Effect.succeed<AttachmentUploadCycleResult>({
164
+ status: "failed",
165
+ step: "resolve-url",
166
+ attachmentId,
167
+ error: new Error("The environment is not connected."),
168
+ });
169
+ }
170
+ return input.transport(url).pipe(
171
+ Effect.as<AttachmentUploadCycleResult>({ status: "uploaded", attachmentId }),
172
+ Effect.catch((error) =>
173
+ Effect.succeed<AttachmentUploadCycleResult>({
174
+ status: "failed",
175
+ step: "transfer",
176
+ attachmentId,
177
+ error,
178
+ }),
179
+ ),
180
+ );
181
+ }),
182
+ ).pipe(
183
+ Effect.onInterrupt(() =>
184
+ Effect.sync(() =>
185
+ deletePendingAttachmentUpload({
186
+ registry: input.registry,
187
+ remove: input.remove,
188
+ environmentId: input.environmentId,
189
+ attachmentId,
190
+ }),
191
+ ),
192
+ ),
193
+ );
194
+ }),
195
+ ).pipe(Effect.withSpan("Attachments.runUploadCycle"));
196
}
197
198
/**