Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadMessageIntake.test.ts

739 lines24.7 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { expect, it } from "@effect/vitest";
6import {
7 ChatAttachmentId,
8 CommandId,
9 EventId,
10 RuntimeRequestId,
11 ThreadId,
12 TurnItemId,
13 type OrchestrationV2Command,
14 type OrchestrationV2StoredEvent,
15 type UserInputAttachments,
16} from "@t3tools/contracts";
17import * as DateTime from "effect/DateTime";
18import * as Effect from "effect/Effect";
19import * as Layer from "effect/Layer";
20import { createPendingAttachmentId, resolveAttachmentPath } from "../attachmentStore.ts";
21import * as ServerConfig from "../config.ts";
22import {
23 OrchestratorCommandPreviouslyRejectedError,
24 OrchestratorDispatchError,
25} from "./Orchestrator.ts";
26import { ThreadManagementService } from "./ThreadManagementService.ts";
27import { dispatchCommand } from "./ThreadMessageIntake.ts";
28​
29const intakeTestLayer = ServerConfig.layerTest(process.cwd(), {
30 prefix: "t3-question-intake-",
31}).pipe(Layer.provideMerge(NodeServices.layer));
32​
33const failingDispatch = (captured: OrchestrationV2Command[]) =>
34 Layer.mock(ThreadManagementService)({
35 dispatch: (command) => {
36 captured.push(command);
37 return Effect.fail(
38 new OrchestratorDispatchError({
39 commandId: command.commandId,
40 commandType: command.type,
41 cause: "Stop after intake",
42 }),
43 );
44 },
45 });
46​
47const NOW = DateTime.makeUnsafe("2026-09-13T00:00:00.000Z");
48​
49const answeredRespondEvents = (input: {
50 readonly threadId: ThreadId;
51 readonly requestId: string;
52 readonly attachmentsByQuestionId: UserInputAttachments;
53}): OrchestrationV2StoredEvent[] => [
54 {
55 sequence: 1,
56 commandId: null,
57 event: {
58 id: EventId.make("evt-recorded-answer"),
59 type: "turn-item.updated",
60 threadId: input.threadId,
61 occurredAt: NOW,
62 payload: {
63 id: TurnItemId.make("item-recorded-answer"),
64 type: "user_input_request",
65 threadId: input.threadId,
66 runId: null,
67 nodeId: null,
68 providerThreadId: null,
69 providerTurnId: null,
70 nativeItemRef: null,
71 parentItemId: null,
72 ordinal: 1,
73 status: "completed",
74 title: null,
75 startedAt: null,
76 completedAt: NOW,
77 updatedAt: NOW,
78 requestId: RuntimeRequestId.make(input.requestId),
79 questions: [],
80 questionAnswer: {
81 requestId: input.requestId,
82 answers: {},
83 attachmentsByQuestionId: input.attachmentsByQuestionId,
84 },
85 },
86 },
87 },
88];
89​
90// The first attempt answered with text only: clients omit attachmentsByQuestionId
91// entirely, so the recorded turn item resolves the request without a questionAnswer.
92const textOnlyRespondEvents = (input: {
93 readonly threadId: ThreadId;
94 readonly requestId: string;
95}): OrchestrationV2StoredEvent[] => [
96 {
97 sequence: 1,
98 commandId: null,
99 event: {
100 id: EventId.make("evt-recorded-answer"),
101 type: "turn-item.updated",
102 threadId: input.threadId,
103 occurredAt: NOW,
104 payload: {
105 id: TurnItemId.make("item-recorded-answer"),
106 type: "user_input_request",
107 threadId: input.threadId,
108 runId: null,
109 nodeId: null,
110 providerThreadId: null,
111 providerTurnId: null,
112 nativeItemRef: null,
113 parentItemId: null,
114 ordinal: 1,
115 status: "completed",
116 title: null,
117 startedAt: null,
118 completedAt: NOW,
119 updatedAt: NOW,
120 requestId: RuntimeRequestId.make(input.requestId),
121 questions: [],
122 },
123 },
124 },
125];
126​
127it.effect("claims question uploads and passes readable paths through the V2 request command", () =>
128 Effect.gen(function* () {
129 const config = yield* ServerConfig.ServerConfig;
130 const id = ChatAttachmentId.make(createPendingAttachmentId()!);
131 NodeFS.writeFileSync(
132 NodePath.join(config.attachmentsDir, `${id}.png`),
133 new Uint8Array([1, 2, 3]),
134 );
135 const captured: OrchestrationV2Command[] = [];
136 yield* dispatchCommand({
137 type: "runtime-request.respond",
138 commandId: CommandId.make("answer-1"),
139 threadId: ThreadId.make("thread-answer"),
140 requestId: RuntimeRequestId.make("request-1"),
141 answers: { q: ["Selected option"] },
142 attachmentsByQuestionId: {
143 q: [{ type: "image", id, name: "screen.png", mimeType: "image/png", sizeBytes: 3 }],
144 },
145 }).pipe(Effect.provide(failingDispatch(captured)), Effect.result);
146 expect(captured).toHaveLength(1);
147 const command = captured[0]!;
148 expect(command.type).toBe("runtime-request.respond");
149 if (command.type !== "runtime-request.respond") return;
150 const attachment = command.attachmentsByQuestionId?.q?.[0];
151 expect(attachment).toBeDefined();
152 const path = resolveAttachmentPath({
153 attachmentsDir: config.attachmentsDir,
154 attachment: attachment!,
155 });
156 expect(path).not.toBeNull();
157 expect(NodeFS.readFileSync(path!)).toEqual(Buffer.from([1, 2, 3]));
158 expect(command.answers?.q).toEqual([
159 "Selected option",
160 `Attached image "screen.png": "${path}"`,
161 ]);
162 }).pipe(Effect.provide(intakeTestLayer)),
163);
164​
165it.effect("rolls back earlier question claims when a later pending upload is missing", () =>
166 Effect.gen(function* () {
167 const config = yield* ServerConfig.ServerConfig;
168 const threadId = ThreadId.make("thread-q-fail");
169 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
170 const existingId = ChatAttachmentId.make("thread-q-fail-00000000-0000-4000-8000-000000000001");
171 NodeFS.writeFileSync(
172 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
173 new Uint8Array([1, 2, 3]),
174 );
175 NodeFS.writeFileSync(
176 NodePath.join(config.attachmentsDir, `${existingId}.png`),
177 new Uint8Array([9, 9, 9, 9]),
178 );
179 const captured: OrchestrationV2Command[] = [];
180 const result = yield* dispatchCommand({
181 type: "runtime-request.respond",
182 commandId: CommandId.make("answer-missing-pending"),
183 threadId,
184 requestId: RuntimeRequestId.make("request-missing-pending"),
185 answers: { q1: ["one"], q2: ["two"] },
186 attachmentsByQuestionId: {
187 q1: [
188 {
189 type: "image",
190 id: existingId,
191 name: "keep.png",
192 mimeType: "image/png",
193 sizeBytes: 4,
194 },
195 {
196 type: "image",
197 id: pendingId,
198 name: "screen.png",
199 mimeType: "image/png",
200 sizeBytes: 3,
201 },
202 ],
203 q2: [
204 {
205 type: "image",
206 id: ChatAttachmentId.make(createPendingAttachmentId()!),
207 name: "gone.png",
208 mimeType: "image/png",
209 sizeBytes: 3,
210 },
211 ],
212 },
213 }).pipe(Effect.provide(failingDispatch(captured)), Effect.result);
214 expect(result._tag).toBe("Failure");
215 if (result._tag === "Failure") {
216 expect(result.failure._tag).toBe("AttachmentClaimError");
217 }
218 expect(captured).toHaveLength(0);
219 // The pending sources stay behind so the user can retry the response.
220 expect(NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toEqual(
221 Buffer.from([1, 2, 3]),
222 );
223 // The pre-existing thread attachment is retained; only the new copy is rolled back.
224 expect(
225 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
226 entry.startsWith("thread-q-fail-"),
227 ),
228 ).toEqual([`${existingId}.png`]);
229 }).pipe(Effect.provide(intakeTestLayer)),
230);
231​
232it.effect("rolls back earlier question claims when attachment path preparation fails", () =>
233 Effect.gen(function* () {
234 const config = yield* ServerConfig.ServerConfig;
235 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
236 NodeFS.writeFileSync(
237 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
238 new Uint8Array([1, 2, 3]),
239 );
240 const captured: OrchestrationV2Command[] = [];
241 const result = yield* dispatchCommand({
242 type: "runtime-request.respond",
243 commandId: CommandId.make("answer-missing-claimed"),
244 threadId: ThreadId.make("thread-path-fail"),
245 requestId: RuntimeRequestId.make("request-missing-claimed"),
246 answers: { q1: ["one"], q2: ["two"] },
247 attachmentsByQuestionId: {
248 q1: [
249 {
250 type: "image",
251 id: pendingId,
252 name: "screen.png",
253 mimeType: "image/png",
254 sizeBytes: 3,
255 },
256 ],
257 q2: [
258 {
259 type: "image",
260 id: ChatAttachmentId.make("thread-path-fail-00000000-0000-4000-8000-000000000099"),
261 name: "missing.png",
262 mimeType: "image/png",
263 sizeBytes: 3,
264 },
265 ],
266 },
267 }).pipe(Effect.provide(failingDispatch(captured)), Effect.result);
268 expect(result._tag).toBe("Failure");
269 if (result._tag === "Failure") {
270 expect(result.failure._tag).toBe("AttachmentClaimError");
271 }
272 expect(captured).toHaveLength(0);
273 expect(NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toEqual(
274 Buffer.from([1, 2, 3]),
275 );
276 expect(
277 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
278 entry.startsWith("thread-path-fail-"),
279 ),
280 ).toEqual([]);
281 }).pipe(Effect.provide(intakeTestLayer)),
282);
283​
284it.effect("retains claimed copies when dispatch failure may have been accepted", () =>
285 Effect.gen(function* () {
286 const config = yield* ServerConfig.ServerConfig;
287 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
288 NodeFS.writeFileSync(
289 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
290 new Uint8Array([1, 2, 3]),
291 );
292 const captured: OrchestrationV2Command[] = [];
293 const result = yield* dispatchCommand({
294 type: "runtime-request.respond",
295 commandId: CommandId.make("answer-ambiguous"),
296 threadId: ThreadId.make("thread-ambiguous"),
297 requestId: RuntimeRequestId.make("request-ambiguous"),
298 answers: { q: ["one"] },
299 attachmentsByQuestionId: {
300 q: [
301 {
302 type: "image",
303 id: pendingId,
304 name: "screen.png",
305 mimeType: "image/png",
306 sizeBytes: 3,
307 },
308 ],
309 },
310 }).pipe(Effect.provide(failingDispatch(captured)), Effect.result);
311 expect(result._tag).toBe("Failure");
312 expect(captured).toHaveLength(1);
313 // The response may have been accepted before the error, so the copy that
314 // backs the answer's path stays for the provider to read.
315 const threadFiles = NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
316 entry.startsWith("thread-ambiguous-"),
317 );
318 expect(threadFiles).toHaveLength(1);
319 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true);
320 }).pipe(Effect.provide(intakeTestLayer)),
321);
322​
323it.effect("releases claimed copies when the command was already rejected", () =>
324 Effect.gen(function* () {
325 const config = yield* ServerConfig.ServerConfig;
326 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
327 NodeFS.writeFileSync(
328 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
329 new Uint8Array([1, 2, 3]),
330 );
331 // A retry of a previously rejected command claims fresh copies, then the
332 // orchestrator replays the rejection without touching them.
333 const result = yield* dispatchCommand({
334 type: "runtime-request.respond",
335 commandId: CommandId.make("answer-rejected"),
336 threadId: ThreadId.make("thread-rejected"),
337 requestId: RuntimeRequestId.make("request-rejected"),
338 answers: { q: ["one"] },
339 attachmentsByQuestionId: {
340 q: [
341 {
342 type: "image",
343 id: pendingId,
344 name: "screen.png",
345 mimeType: "image/png",
346 sizeBytes: 3,
347 },
348 ],
349 },
350 }).pipe(
351 Effect.provide(
352 Layer.mock(ThreadManagementService)({
353 dispatch: (command) =>
354 Effect.fail(
355 new OrchestratorCommandPreviouslyRejectedError({
356 commandId: command.commandId,
357 commandType: command.type,
358 detail: "rejected earlier",
359 }),
360 ),
361 }),
362 ),
363 Effect.result,
364 );
365 expect(result._tag).toBe("Failure");
366 expect(
367 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
368 entry.startsWith("thread-rejected-"),
369 ),
370 ).toEqual([]);
371 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true);
372 }).pipe(Effect.provide(intakeTestLayer)),
373);
374​
375it.effect("releases claimed copies when dispatch replays an earlier accepted response", () =>
376 Effect.gen(function* () {
377 const config = yield* ServerConfig.ServerConfig;
378 const threadId = ThreadId.make("thread-replay");
379 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
380 NodeFS.writeFileSync(
381 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
382 new Uint8Array([1, 2, 3]),
383 );
384 // The first attempt was accepted but its response was lost. The retry's
385 // dispatch replays the stored events, which reference the first claim's
386 // attachment id — not this attempt's fresh copy.
387 const earlierAttachment = {
388 type: "image" as const,
389 id: ChatAttachmentId.make("thread-replay-00000000-0000-4000-8000-0000000000aa"),
390 name: "screen.png",
391 mimeType: "image/png",
392 sizeBytes: 3,
393 };
394 NodeFS.writeFileSync(
395 NodePath.join(config.attachmentsDir, `${earlierAttachment.id}.png`),
396 new Uint8Array([4, 4, 4]),
397 );
398 const storedEvents = answeredRespondEvents({
399 threadId,
400 requestId: "request-replay",
401 attachmentsByQuestionId: { q: [earlierAttachment] },
402 });
403 const result = yield* dispatchCommand({
404 type: "runtime-request.respond",
405 commandId: CommandId.make("answer-replay"),
406 threadId,
407 requestId: RuntimeRequestId.make("request-replay"),
408 answers: { q: ["one"] },
409 attachmentsByQuestionId: {
410 q: [
411 {
412 type: "image",
413 id: pendingId,
414 name: "screen.png",
415 mimeType: "image/png",
416 sizeBytes: 3,
417 },
418 ],
419 },
420 }).pipe(
421 Effect.provide(
422 Layer.mock(ThreadManagementService)({
423 dispatch: () => Effect.succeed({ sequence: 1, storedEvents }),
424 }),
425 ),
426 Effect.result,
427 );
428 expect(result._tag).toBe("Success");
429 // The accepted copy from the first attempt is untouched; only the
430 // retry's unreferenced copy is removed.
431 expect(
432 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
433 entry.startsWith("thread-replay-"),
434 ),
435 ).toEqual([`${earlierAttachment.id}.png`]);
436 expect(
437 NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${earlierAttachment.id}.png`)),
438 ).toEqual(Buffer.from([4, 4, 4]));
439 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true);
440 }).pipe(Effect.provide(intakeTestLayer)),
441);
442​
443it.effect("releases claimed copies when the recorded answer has no attachments", () =>
444 Effect.gen(function* () {
445 const config = yield* ServerConfig.ServerConfig;
446 const threadId = ThreadId.make("thread-empty-answer");
447 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
448 NodeFS.writeFileSync(
449 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
450 new Uint8Array([1, 2, 3]),
451 );
452 // The same command id was first answered without attachments; this
453 // retry's copies are unreferenced by the recorded answer.
454 const storedEvents = answeredRespondEvents({
455 threadId,
456 requestId: "request-empty-answer",
457 attachmentsByQuestionId: {},
458 });
459 const result = yield* dispatchCommand({
460 type: "runtime-request.respond",
461 commandId: CommandId.make("answer-empty"),
462 threadId,
463 requestId: RuntimeRequestId.make("request-empty-answer"),
464 answers: { q: ["one"] },
465 attachmentsByQuestionId: {
466 q: [
467 {
468 type: "image",
469 id: pendingId,
470 name: "screen.png",
471 mimeType: "image/png",
472 sizeBytes: 3,
473 },
474 ],
475 },
476 }).pipe(
477 Effect.provide(
478 Layer.mock(ThreadManagementService)({
479 dispatch: () => Effect.succeed({ sequence: 1, storedEvents }),
480 }),
481 ),
482 Effect.result,
483 );
484 expect(result._tag).toBe("Success");
485 expect(
486 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
487 entry.startsWith("thread-empty-answer-"),
488 ),
489 ).toEqual([]);
490 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true);
491 }).pipe(Effect.provide(intakeTestLayer)),
492);
493​
494it.effect("releases claimed copies when the recorded answer was text-only", () =>
495 Effect.gen(function* () {
496 const config = yield* ServerConfig.ServerConfig;
497 const threadId = ThreadId.make("thread-text-only-answer");
498 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
499 NodeFS.writeFileSync(
500 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
501 new Uint8Array([1, 2, 3]),
502 );
503 // The first attempt accepted a text-only response, so its recorded turn
504 // item has no questionAnswer at all. The retried command id uploads an
505 // attachment whose fresh copy is unreferenced by that answer.
506 const storedEvents = textOnlyRespondEvents({
507 threadId,
508 requestId: "request-text-only",
509 });
510 const result = yield* dispatchCommand({
511 type: "runtime-request.respond",
512 commandId: CommandId.make("answer-text-only"),
513 threadId,
514 requestId: RuntimeRequestId.make("request-text-only"),
515 answers: { q: ["one"] },
516 attachmentsByQuestionId: {
517 q: [
518 {
519 type: "image",
520 id: pendingId,
521 name: "screen.png",
522 mimeType: "image/png",
523 sizeBytes: 3,
524 },
525 ],
526 },
527 }).pipe(
528 Effect.provide(
529 Layer.mock(ThreadManagementService)({
530 dispatch: () => Effect.succeed({ sequence: 1, storedEvents }),
531 }),
532 ),
533 Effect.result,
534 );
535 expect(result._tag).toBe("Success");
536 expect(
537 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
538 entry.startsWith("thread-text-only-answer-"),
539 ),
540 ).toEqual([]);
541 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true);
542 }).pipe(Effect.provide(intakeTestLayer)),
543);
544​
545it.effect("retains claimed copies referenced by a fresh recorded answer", () =>
546 Effect.gen(function* () {
547 const config = yield* ServerConfig.ServerConfig;
548 const threadId = ThreadId.make("thread-fresh-answer");
549 const firstId = ChatAttachmentId.make(createPendingAttachmentId()!);
550 const secondId = ChatAttachmentId.make(createPendingAttachmentId()!);
551 NodeFS.writeFileSync(
552 NodePath.join(config.attachmentsDir, `${firstId}.png`),
553 new Uint8Array([1, 2, 3]),
554 );
555 NodeFS.writeFileSync(
556 NodePath.join(config.attachmentsDir, `${secondId}.png`),
557 new Uint8Array([7, 7]),
558 );
559 const result = yield* dispatchCommand({
560 type: "runtime-request.respond",
561 commandId: CommandId.make("answer-fresh"),
562 threadId,
563 requestId: RuntimeRequestId.make("request-fresh"),
564 answers: { q1: ["one"], q2: ["two"] },
565 attachmentsByQuestionId: {
566 q1: [
567 {
568 type: "image",
569 id: firstId,
570 name: "one.png",
571 mimeType: "image/png",
572 sizeBytes: 3,
573 },
574 ],
575 q2: [
576 {
577 type: "image",
578 id: secondId,
579 name: "two.png",
580 mimeType: "image/png",
581 sizeBytes: 2,
582 },
583 ],
584 },
585 }).pipe(
586 Effect.provide(
587 Layer.mock(ThreadManagementService)({
588 // A fresh commit records exactly the attachments this attempt claimed.
589 dispatch: (command) =>
590 Effect.succeed({
591 sequence: 1,
592 storedEvents: answeredRespondEvents({
593 threadId,
594 requestId: "request-fresh",
595 attachmentsByQuestionId:
596 command.type === "runtime-request.respond"
597 ? (command.attachmentsByQuestionId ?? {})
598 : {},
599 }),
600 }),
601 }),
602 ),
603 Effect.result,
604 );
605 expect(result._tag).toBe("Success");
606 expect(
607 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
608 entry.startsWith("thread-fresh-answer-"),
609 ),
610 ).toHaveLength(2);
611 }).pipe(Effect.provide(intakeTestLayer)),
612);
613​
614it.effect("releases claimed copies when preparation dies with a defect", () =>
615 Effect.gen(function* () {
616 const config = yield* ServerConfig.ServerConfig;
617 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
618 NodeFS.writeFileSync(
619 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
620 new Uint8Array([1, 2, 3]),
621 );
622 const poisoned = {
623 get id(): string {
624 throw new Error("poisoned");
625 },
626 };
627 const captured: OrchestrationV2Command[] = [];
628 const result = yield* dispatchCommand({
629 type: "runtime-request.respond",
630 commandId: CommandId.make("answer-defect"),
631 threadId: ThreadId.make("thread-defect"),
632 requestId: RuntimeRequestId.make("request-defect"),
633 answers: { q1: ["one"], q2: ["two"] },
634 attachmentsByQuestionId: {
635 q1: [
636 {
637 type: "image",
638 id: pendingId,
639 name: "screen.png",
640 mimeType: "image/png",
641 sizeBytes: 3,
642 },
643 ],
644 q2: [poisoned] as never,
645 },
646 }).pipe(Effect.provide(failingDispatch(captured)), Effect.exit);
647 expect(result._tag).toBe("Failure");
648 expect(captured).toHaveLength(0);
649 expect(
650 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
651 entry.startsWith("thread-defect-"),
652 ),
653 ).toEqual([]);
654 expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true);
655 }).pipe(Effect.provide(intakeTestLayer)),
656);
657​
658it.effect("a retried response re-claims the preserved pending uploads", () =>
659 Effect.gen(function* () {
660 const config = yield* ServerConfig.ServerConfig;
661 const threadId = ThreadId.make("thread-retry");
662 const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!);
663 const lateId = ChatAttachmentId.make(createPendingAttachmentId()!);
664 NodeFS.writeFileSync(
665 NodePath.join(config.attachmentsDir, `${pendingId}.png`),
666 new Uint8Array([1, 2, 3]),
667 );
668 const command: OrchestrationV2Command = {
669 type: "runtime-request.respond",
670 commandId: CommandId.make("answer-retry"),
671 threadId,
672 requestId: RuntimeRequestId.make("request-retry"),
673 answers: { q1: ["one"], q2: ["two"] },
674 attachmentsByQuestionId: {
675 q1: [
676 {
677 type: "image",
678 id: pendingId,
679 name: "screen.png",
680 mimeType: "image/png",
681 sizeBytes: 3,
682 },
683 ],
684 q2: [
685 {
686 type: "image",
687 id: lateId,
688 name: "late.png",
689 mimeType: "image/png",
690 sizeBytes: 2,
691 },
692 ],
693 },
694 };
695 const captured: OrchestrationV2Command[] = [];
696 const first = yield* dispatchCommand(command).pipe(
697 Effect.provide(failingDispatch(captured)),
698 Effect.result,
699 );
700 expect(first._tag).toBe("Failure");
701 expect(captured).toHaveLength(0);
702​
703 NodeFS.writeFileSync(
704 NodePath.join(config.attachmentsDir, `${lateId}.png`),
705 new Uint8Array([7, 7]),
706 );
707 const retry = yield* dispatchCommand(command).pipe(
708 Effect.provide(
709 Layer.mock(ThreadManagementService)({
710 dispatch: (dispatched) => {
711 captured.push(dispatched);
712 return Effect.succeed({ sequence: 1, storedEvents: [] });
713 },
714 }),
715 ),
716 Effect.result,
717 );
718 expect(retry._tag).toBe("Success");
719 const dispatched = captured[0]!;
720 expect(dispatched.type).toBe("runtime-request.respond");
721 if (dispatched.type !== "runtime-request.respond") return;
722 for (const attachment of [
723 ...(dispatched.attachmentsByQuestionId?.q1 ?? []),
724 ...(dispatched.attachmentsByQuestionId?.q2 ?? []),
725 ]) {
726 const path = resolveAttachmentPath({
727 attachmentsDir: config.attachmentsDir,
728 attachment,
729 });
730 expect(path).not.toBeNull();
731 expect(NodeFS.existsSync(path!)).toBe(true);
732 }
733 expect(
734 NodeFS.readdirSync(config.attachmentsDir).filter((entry) =>
735 entry.startsWith("thread-retry-"),
736 ),
737 ).toHaveLength(2);
738 }).pipe(Effect.provide(intakeTestLayer)),
739);