Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { describe, expect, it } from "@effect/vitest";
7import {
8 ClientOrchestrationCommand,
9 CommandId,
10 ApprovalRequestId,
11 MessageId,
12 type OrchestrationMessageContext,
13 ThreadId,
14} from "@t3tools/contracts";
15import * as Effect from "effect/Effect";
16import * as Layer from "effect/Layer";
17import * as Schema from "effect/Schema";
18
19import * as ServerConfig from "../config.ts";
20import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
21import { cleanupFailedUploadedAttachments, normalizeDispatchCommand } from "./Normalizer.ts";
22
23const testLayer = Layer.mergeAll(
24 WorkspacePaths.layer,
25 ServerConfig.layerTest(process.cwd(), { prefix: "t3-normalizer-attachments-" }),
26).pipe(Layer.provideMerge(NodeServices.layer));
27
28const attachmentUuid = "00000000-0000-4000-8000-0000000000aa";
29const isClientCommand = Schema.is(ClientOrchestrationCommand);
30
31function turnStartCommand(input: {
32 readonly threadId?: string;
33 readonly attachments: ReadonlyArray<
34 | { readonly id: string; readonly sizeBytes: number }
35 | { readonly dataUrl: string; readonly sizeBytes: number; readonly id?: string }
36 >;
37 readonly context?: OrchestrationMessageContext;
38}): ClientOrchestrationCommand {
39 return {
40 type: "thread.turn.start",
41 commandId: CommandId.make("command-1"),
42 threadId: ThreadId.make(input.threadId ?? "thread-1"),
43 message: {
44 messageId: MessageId.make("message-1"),
45 role: "user",
46 text: "look at this",
47 attachments: input.attachments.map((attachment) => ({
48 type: "image" as const,
49 name: "screenshot.png",
50 mimeType: "image/png",
51 ...attachment,
52 })),
53 ...(input.context !== undefined ? { context: input.context } : {}),
54 },
55 runtimeMode: "full-access",
56 interactionMode: "default",
57 createdAt: "2026-08-01T00:00:00.000Z",
58 };
59}
60
61describe("normalizeDispatchCommand attachments", () => {
62 it.effect("rejects duplicate client ids before persisting attachments", () =>
63 Effect.gen(function* () {
64 const error = yield* normalizeDispatchCommand(
65 turnStartCommand({
66 attachments: [
67 { id: "same", dataUrl: "data:image/png;base64,cGl4ZWxz", sizeBytes: 6 },
68 { id: "same", dataUrl: "data:image/png;base64,b3RoZXI=", sizeBytes: 5 },
69 ],
70 }),
71 ).pipe(Effect.flip);
72 expect(error.message).toContain("duplicate attachment id");
73 const config = yield* ServerConfig.ServerConfig;
74 expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([]);
75 }).pipe(Effect.provide(testLayer)),
76 );
77
78 it.effect("rebinds image context records from the client id to the persisted id", () =>
79 Effect.gen(function* () {
80 const normalized = yield* normalizeDispatchCommand(
81 turnStartCommand({
82 attachments: [
83 { id: "local-image-1", dataUrl: "data:image/png;base64,cGl4ZWxz", sizeBytes: 6 },
84 ],
85 context: {
86 version: 1,
87 records: [
88 {
89 version: 1,
90 contextId: "local-image-1" as never,
91 kind: "image",
92 label: "screenshot.png",
93 attachmentId: "local-image-1",
94 name: "screenshot.png",
95 mimeType: "image/png",
96 sizeBytes: 6,
97 },
98 {
99 version: 1,
100 contextId: "ctx-skill" as never,
101 kind: "skill",
102 label: "$review",
103 name: "review",
104 },
105 ],
106 },
107 }),
108 );
109 if (normalized.type !== "thread.turn.start") {
110 throw new Error("Expected a thread.turn.start command.");
111 }
112 const persistedId = normalized.message.attachments[0]!.id;
113 expect(persistedId.startsWith("thread-1-")).toBe(true);
114 const records = normalized.message.context?.records ?? [];
115 expect(records[0]).toMatchObject({
116 kind: "image",
117 contextId: "local-image-1",
118 attachmentId: persistedId,
119 });
120 expect(records[1]).toMatchObject({ kind: "skill", name: "review" });
121 }).pipe(Effect.provide(testLayer)),
122 );
123
124 it.effect("preserves inline image attachments from existing mobile clients", () =>
125 Effect.gen(function* () {
126 const config = yield* ServerConfig.ServerConfig;
127 const normalized = yield* normalizeDispatchCommand(
128 turnStartCommand({
129 attachments: [{ dataUrl: "data:image/png;base64,cGl4ZWxz", sizeBytes: 6 }],
130 }),
131 );
132 if (normalized.type !== "thread.turn.start") {
133 throw new Error("Expected a thread.turn.start command.");
134 }
135
136 const attachment = normalized.message.attachments[0]!;
137 expect(attachment.id.startsWith("thread-1-")).toBe(true);
138 expect(
139 NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${attachment.id}.png`)),
140 ).toEqual(Buffer.from("pixels"));
141 }).pipe(Effect.provide(testLayer)),
142 );
143
144 it.effect("claims uploaded attachments while retaining a retryable pending copy", () =>
145 Effect.gen(function* () {
146 const config = yield* ServerConfig.ServerConfig;
147 const bytes = Buffer.from("pixels");
148 const pendingPath = NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`);
149 NodeFS.writeFileSync(pendingPath, bytes);
150
151 const normalized = yield* normalizeDispatchCommand(
152 turnStartCommand({
153 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: bytes.byteLength }],
154 context: {
155 version: 1,
156 records: [
157 {
158 version: 1,
159 contextId: "ctx_pending" as never,
160 kind: "image",
161 label: "upload.png",
162 attachmentId: `pending-${attachmentUuid}`,
163 name: "upload.png",
164 mimeType: "image/png",
165 sizeBytes: bytes.byteLength,
166 },
167 ],
168 },
169 }),
170 );
171 if (normalized.type !== "thread.turn.start") {
172 throw new Error("Expected a thread.turn.start command.");
173 }
174
175 const attachmentId = normalized.message.attachments[0]!.id;
176 expect(attachmentId.startsWith("thread-1-")).toBe(true);
177 expect(attachmentId).not.toBe(`thread-1-${attachmentUuid}`);
178 expect(normalized.message.context?.records[0]).toMatchObject({ attachmentId });
179 expect(NodeFS.existsSync(pendingPath)).toBe(true);
180 const claimedPngPath = NodePath.join(config.attachmentsDir, `${attachmentId}.png`);
181 expect(NodeFS.existsSync(claimedPngPath)).toBe(true);
182 // A copy, not a hard link: editing the delivered file must not mutate
183 // the retryable pending upload.
184 expect(NodeFS.statSync(claimedPngPath).ino).not.toBe(NodeFS.statSync(pendingPath).ino);
185 expect(NodeFS.readFileSync(claimedPngPath)).toEqual(bytes);
186 }).pipe(Effect.provide(testLayer)),
187 );
188
189 it.effect("normalizes inline and uploaded attachments in the same turn", () =>
190 Effect.gen(function* () {
191 const config = yield* ServerConfig.ServerConfig;
192 NodeFS.writeFileSync(
193 NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`),
194 Buffer.from("pixels"),
195 );
196
197 const normalized = yield* normalizeDispatchCommand(
198 turnStartCommand({
199 attachments: [
200 { dataUrl: "data:image/png;base64,cGl4ZWxz", sizeBytes: 6 },
201 { id: `pending-${attachmentUuid}`, sizeBytes: 6 },
202 ],
203 }),
204 );
205 if (normalized.type !== "thread.turn.start") {
206 throw new Error("Expected a thread.turn.start command.");
207 }
208
209 expect(normalized.message.attachments).toHaveLength(2);
210 expect(normalized.message.attachments[1]?.id.startsWith("thread-1-")).toBe(true);
211 }).pipe(Effect.provide(testLayer)),
212 );
213
214 it.effect("claims uploaded documents without changing their original extension", () =>
215 Effect.gen(function* () {
216 const config = yield* ServerConfig.ServerConfig;
217 const pendingId = `pending-${attachmentUuid}-pdf`;
218 const pendingPath = NodePath.join(config.attachmentsDir, `${pendingId}.pdf`);
219 NodeFS.writeFileSync(pendingPath, Buffer.from("report"));
220
221 const imageCommand = turnStartCommand({ attachments: [] });
222 if (imageCommand.type !== "thread.turn.start") {
223 throw new Error("Expected a thread.turn.start command.");
224 }
225 const normalized = yield* normalizeDispatchCommand({
226 ...imageCommand,
227 message: {
228 ...imageCommand.message,
229 attachments: [
230 {
231 type: "file",
232 id: pendingId,
233 name: "report.pdf",
234 mimeType: "application/pdf",
235 sizeBytes: 6,
236 },
237 ],
238 },
239 });
240 if (normalized.type !== "thread.turn.start") {
241 throw new Error("Expected a thread.turn.start command.");
242 }
243
244 const attachment = normalized.message.attachments[0]!;
245 expect(attachment.type).toBe("file");
246 expect(attachment.id).toMatch(/^thread-1-.*-pdf$/);
247 const claimedPath = NodePath.join(config.attachmentsDir, `${attachment.id}.pdf`);
248 expect(NodeFS.readFileSync(claimedPath)).toEqual(Buffer.from("report"));
249 expect(NodeFS.statSync(claimedPath).ino).not.toBe(NodeFS.statSync(pendingPath).ino);
250 }).pipe(Effect.provide(testLayer)),
251 );
252
253 it.effect("retries a failed bootstrap with a fresh thread id", () =>
254 Effect.gen(function* () {
255 const config = yield* ServerConfig.ServerConfig;
256 const bytes = Buffer.from("pixels");
257 NodeFS.writeFileSync(
258 NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`),
259 bytes,
260 );
261
262 const first = yield* normalizeDispatchCommand(
263 turnStartCommand({
264 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: bytes.byteLength }],
265 }),
266 );
267 if (first.type !== "thread.turn.start") {
268 throw new Error("Expected a thread.turn.start command.");
269 }
270 NodeFS.rmSync(
271 NodePath.join(config.attachmentsDir, `${first.message.attachments[0]!.id}.png`),
272 );
273
274 const retried = yield* normalizeDispatchCommand(
275 turnStartCommand({
276 threadId: "thread-retry",
277 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: bytes.byteLength }],
278 }),
279 );
280 if (retried.type !== "thread.turn.start") {
281 throw new Error("Expected a thread.turn.start command.");
282 }
283 expect(retried.message.attachments[0]?.id.startsWith("thread-retry-")).toBe(true);
284 }).pipe(Effect.provide(testLayer)),
285 );
286
287 it.effect("removes failed attachment claims without deleting their pending uploads", () =>
288 Effect.gen(function* () {
289 const config = yield* ServerConfig.ServerConfig;
290 const pendingPath = NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`);
291 NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
292 const command = turnStartCommand({
293 attachments: [
294 { dataUrl: "data:image/png;base64,cGl4ZWxz", sizeBytes: 6 },
295 { id: `pending-${attachmentUuid}`, sizeBytes: 6 },
296 ],
297 });
298 const normalized = yield* normalizeDispatchCommand(command);
299 if (normalized.type !== "thread.turn.start") {
300 throw new Error("Expected a thread.turn.start command.");
301 }
302
303 const inlinePath = NodePath.join(
304 config.attachmentsDir,
305 `${normalized.message.attachments[0]!.id}.png`,
306 );
307 const claimedPath = NodePath.join(
308 config.attachmentsDir,
309 `${normalized.message.attachments[1]!.id}.png`,
310 );
311 yield* cleanupFailedUploadedAttachments(command, normalized);
312
313 expect(NodeFS.existsSync(pendingPath)).toBe(true);
314 expect(NodeFS.existsSync(claimedPath)).toBe(false);
315 expect(NodeFS.existsSync(inlinePath)).toBe(true);
316 }).pipe(Effect.provide(testLayer)),
317 );
318
319 it.effect("removes a failed claimed copy after its pending original was removed", () =>
320 Effect.gen(function* () {
321 const config = yield* ServerConfig.ServerConfig;
322 const pendingPath = NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`);
323 NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
324 const command = turnStartCommand({
325 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: 6 }],
326 });
327 const normalized = yield* normalizeDispatchCommand(command);
328 if (normalized.type !== "thread.turn.start") {
329 throw new Error("Expected a thread.turn.start command.");
330 }
331
332 const claimedPath = NodePath.join(
333 config.attachmentsDir,
334 `${normalized.message.attachments[0]!.id}.png`,
335 );
336 NodeFS.rmSync(pendingPath);
337
338 yield* cleanupFailedUploadedAttachments(command, normalized);
339
340 expect(NodeFS.existsSync(claimedPath)).toBe(false);
341 }).pipe(Effect.provide(testLayer)),
342 );
343
344 it.effect("keeps concurrent claims independent when one dispatch fails", () =>
345 Effect.gen(function* () {
346 const config = yield* ServerConfig.ServerConfig;
347 const pendingPath = NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`);
348 NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
349 const command = turnStartCommand({
350 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: 6 }],
351 });
352
353 const [failed, succeeded] = yield* Effect.all(
354 [normalizeDispatchCommand(command), normalizeDispatchCommand(command)],
355 { concurrency: 2 },
356 );
357 if (failed.type !== "thread.turn.start" || succeeded.type !== "thread.turn.start") {
358 throw new Error("Expected thread.turn.start commands.");
359 }
360
361 const failedPath = NodePath.join(
362 config.attachmentsDir,
363 `${failed.message.attachments[0]!.id}.png`,
364 );
365 const succeededPath = NodePath.join(
366 config.attachmentsDir,
367 `${succeeded.message.attachments[0]!.id}.png`,
368 );
369 expect(failedPath).not.toBe(succeededPath);
370
371 yield* cleanupFailedUploadedAttachments(command, failed);
372
373 expect(NodeFS.existsSync(pendingPath)).toBe(true);
374 expect(NodeFS.existsSync(failedPath)).toBe(false);
375 expect(NodeFS.existsSync(succeededPath)).toBe(true);
376 }).pipe(Effect.provide(testLayer)),
377 );
378
379 it.effect("removes earlier claimed copies when a later attachment cannot be normalized", () =>
380 Effect.gen(function* () {
381 const config = yield* ServerConfig.ServerConfig;
382 const pendingId = `pending-${attachmentUuid}`;
383 const pendingPath = NodePath.join(config.attachmentsDir, `${pendingId}.png`);
384 NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
385
386 const failure = yield* normalizeDispatchCommand(
387 turnStartCommand({
388 attachments: [
389 { id: pendingId, sizeBytes: 6 },
390 {
391 id: "pending-00000000-0000-4000-8000-0000000000ff",
392 sizeBytes: 6,
393 },
394 ],
395 }),
396 ).pipe(Effect.flip);
397
398 expect(failure.message).toContain("not found");
399 expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([`${pendingId}.png`]);
400 }).pipe(Effect.provide(testLayer)),
401 );
402
403 it.effect("rejects uploaded attachments with the wrong size or thread", () =>
404 Effect.gen(function* () {
405 const config = yield* ServerConfig.ServerConfig;
406 NodeFS.writeFileSync(
407 NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`),
408 Buffer.from("pixels"),
409 );
410
411 const wrongSize = yield* normalizeDispatchCommand(
412 turnStartCommand({
413 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: 999 }],
414 }),
415 ).pipe(Effect.flip);
416 expect(wrongSize.message).toContain("size");
417
418 const wrongThread = yield* normalizeDispatchCommand(
419 turnStartCommand({
420 attachments: [{ id: `another-thread-${attachmentUuid}`, sizeBytes: 6 }],
421 }),
422 ).pipe(Effect.flip);
423 expect(wrongThread.message).toContain("pending upload");
424
425 const mismatchedTypeCommand = turnStartCommand({
426 attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: 6 }],
427 });
428 if (mismatchedTypeCommand.type !== "thread.turn.start") {
429 throw new Error("Expected a thread.turn.start command.");
430 }
431 const mismatchedType = yield* normalizeDispatchCommand({
432 ...mismatchedTypeCommand,
433 message: {
434 ...mismatchedTypeCommand.message,
435 attachments: mismatchedTypeCommand.message.attachments.map((attachment) => ({
436 ...attachment,
437 mimeType: "image/jpeg",
438 })),
439 },
440 }).pipe(Effect.flip);
441 expect(mismatchedType.message).toContain("attachment type");
442 }).pipe(Effect.provide(testLayer)),
443 );
444});
445
446describe("question attachments", () => {
447 it.effect("enforces the total response limit and claims duplicate filenames independently", () =>
448 Effect.gen(function* () {
449 const config = yield* ServerConfig.ServerConfig;
450 const id = `pending-${attachmentUuid}-txt`;
451 NodeFS.writeFileSync(NodePath.join(config.attachmentsDir, `${id}.txt`), "report");
452 const attachment = {
453 type: "file" as const,
454 id,
455 name: 'notes "final" ü.txt',
456 mimeType: "text/plain",
457 sizeBytes: 6,
458 };
459 const command: ClientOrchestrationCommand = {
460 type: "thread.user-input.respond",
461 commandId: CommandId.make("answer-cap"),
462 threadId: ThreadId.make("thread-1"),
463 requestId: ApprovalRequestId.make("request-cap"),
464 answers: { first: "", second: "" },
465 createdAt: "2026-08-01T00:00:00.000Z",
466 attachmentsByQuestionId: {
467 first: Array.from({ length: 4 }, () => attachment),
468 second: Array.from({ length: 5 }, () => attachment),
469 },
470 };
471 const failure = yield* normalizeDispatchCommand(command).pipe(Effect.flip);
472 expect(failure.message).toContain("up to 8");
473 expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([`${id}.txt`]);
474 const accepted = {
475 ...command,
476 attachmentsByQuestionId: {
477 ...command.attachmentsByQuestionId,
478 second: Array.from({ length: 4 }, () => attachment),
479 },
480 };
481 const normalized = yield* normalizeDispatchCommand(accepted);
482 if (normalized.type !== "thread.user-input.respond") throw new Error("Wrong command");
483 const attachments = Object.values(normalized.attachmentsByQuestionId!).flat();
484 expect(attachments).toHaveLength(8);
485 expect(new Set(attachments.map((item) => item.id)).size).toBe(8);
486 for (const item of attachments) {
487 expect(item.name).toBe(attachment.name);
488 expect(
489 NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${item.id}.txt`), "utf8"),
490 ).toBe("report");
491 }
492 yield* cleanupFailedUploadedAttachments(accepted, normalized);
493 expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([`${id}.txt`]);
494 }).pipe(Effect.provide(testLayer)),
495 );
496 it("requires uploaded metadata for question images, including pasted images", () => {
497 expect(
498 isClientCommand({
499 type: "thread.user-input.respond",
500 commandId: "answer",
501 threadId: "thread-1",
502 requestId: "request",
503 answers: { q: "" },
504 createdAt: "2026-08-01T00:00:00.000Z",
505 attachmentsByQuestionId: {
506 q: [
507 {
508 type: "image",
509 name: "image.png",
510 mimeType: "image/png",
511 sizeBytes: 6,
512 dataUrl: "data:image/png;base64,cGl4ZWxz",
513 },
514 ],
515 },
516 }),
517 ).toBe(false);
518 });
519
520 it.effect("preserves a __proto__ question key and cleans up its claimed files", () =>
521 Effect.gen(function* () {
522 const config = yield* ServerConfig.ServerConfig;
523 const id = `pending-${attachmentUuid}`;
524 NodeFS.writeFileSync(NodePath.join(config.attachmentsDir, `${id}.png`), "pixels");
525 const command: ClientOrchestrationCommand = {
526 type: "thread.user-input.respond",
527 commandId: CommandId.make("answer"),
528 threadId: ThreadId.make("thread-1"),
529 requestId: ApprovalRequestId.make("request"),
530 answers: { ["__proto__"]: "" },
531 createdAt: "2026-08-01T00:00:00.000Z",
532 attachmentsByQuestionId: {
533 ["__proto__"]: [
534 { type: "image", id, name: "image.png", mimeType: "image/png", sizeBytes: 6 },
535 ],
536 },
537 };
538 const normalized = yield* normalizeDispatchCommand(command);
539 if (normalized.type !== "thread.user-input.respond") throw new Error("Wrong command");
540 expect(Object.keys(normalized.attachmentsByQuestionId!)).toEqual(["__proto__"]);
541 const attachment = normalized.attachmentsByQuestionId!["__proto__"]![0]!;
542 const claimedPath = NodePath.join(config.attachmentsDir, `${attachment.id}.png`);
543 expect(NodeFS.existsSync(claimedPath)).toBe(true);
544 yield* cleanupFailedUploadedAttachments(command, normalized);
545 expect(NodeFS.existsSync(claimedPath)).toBe(false);
546 }).pipe(Effect.provide(testLayer)),
547 );
548 it.effect(
549 "claims images and files by question, preserves answers, and cleans up failed dispatches",
550 () =>
551 Effect.gen(function* () {
552 const config = yield* ServerConfig.ServerConfig;
553 const imageId = `pending-${attachmentUuid}`;
554 const fileId = `pending-${attachmentUuid}-txt`;
555 NodeFS.writeFileSync(NodePath.join(config.attachmentsDir, `${imageId}.png`), "pixels");
556 NodeFS.writeFileSync(NodePath.join(config.attachmentsDir, `${fileId}.txt`), "report");
557 const command: ClientOrchestrationCommand = {
558 type: "thread.user-input.respond",
559 commandId: CommandId.make("answer"),
560 threadId: ThreadId.make("thread-1"),
561 requestId: ApprovalRequestId.make("request"),
562 answers: { q1: ["Selected option"], q2: "" },
563 createdAt: "2026-08-01T00:00:00.000Z",
564 attachmentsByQuestionId: {
565 q1: [
566 {
567 type: "image",
568 id: imageId,
569 name: "image.png",
570 mimeType: "image/png",
571 sizeBytes: 6,
572 },
573 ],
574 q2: [
575 {
576 type: "file",
577 id: fileId,
578 name: "report.txt",
579 mimeType: "text/plain",
580 sizeBytes: 6,
581 },
582 ],
583 },
584 };
585 const normalized = yield* normalizeDispatchCommand(command);
586 if (normalized.type !== "thread.user-input.respond") throw new Error("Wrong command");
587 expect(normalized.answers).toEqual(command.answers);
588 const image = normalized.attachmentsByQuestionId!.q1![0]!;
589 const file = normalized.attachmentsByQuestionId!.q2![0]!;
590 expect(
591 NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${image.id}.png`), "utf8"),
592 ).toBe("pixels");
593 expect(
594 NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${file.id}.txt`), "utf8"),
595 ).toBe("report");
596 yield* cleanupFailedUploadedAttachments(command, normalized);
597 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${image.id}.png`))).toBe(
598 false,
599 );
600 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${file.id}.txt`))).toBe(
601 false,
602 );
603 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${imageId}.png`))).toBe(
604 true,
605 );
606 const retry = yield* normalizeDispatchCommand(command);
607 expect(retry.type).toBe("thread.user-input.respond");
608 }).pipe(Effect.provide(testLayer)),
609 );
610
611 it.effect("removes all claimed copies if a later question upload is missing", () =>
612 Effect.gen(function* () {
613 const config = yield* ServerConfig.ServerConfig;
614 const id = `pending-${attachmentUuid}`;
615 NodeFS.writeFileSync(NodePath.join(config.attachmentsDir, `${id}.png`), "pixels");
616 const result = yield* normalizeDispatchCommand({
617 type: "thread.user-input.respond",
618 commandId: CommandId.make("answer"),
619 threadId: ThreadId.make("thread-1"),
620 requestId: ApprovalRequestId.make("request"),
621 answers: { q1: "", q2: "" },
622 createdAt: "2026-08-01T00:00:00.000Z",
623 attachmentsByQuestionId: {
624 q1: [{ type: "image", id, name: "image.png", mimeType: "image/png", sizeBytes: 6 }],
625 q2: [
626 {
627 type: "file",
628 id: `${id}-txt`,
629 name: "missing.txt",
630 mimeType: "text/plain",
631 sizeBytes: 6,
632 },
633 ],
634 },
635 }).pipe(Effect.result);
636 expect(result._tag).toBe("Failure");
637 expect(
638 NodeFS.readdirSync(config.attachmentsDir).filter((name) => name.startsWith("thread-1-")),
639 ).toEqual([]);
640 }).pipe(Effect.provide(testLayer)),
641 );
642});