TypeScript
fix(server): thread deletion removes files attached to question answers (#26074)
Fuksssf56a4775fd9e
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.tsindex 805828064b4..84c650f75fe 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts@@ -5319,6 +5319,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =WHERE thread_id = ${threadId}AND type = 'dynamic_tool'AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`})+ `,+ sql<{ id: string }>`+ SELECT DISTINCT json_extract(attachment.value, '$.id') AS id+ FROM orchestration_v2_projection_turn_items AS item,+ json_each(item.payload_json, '$.questionAnswer.attachmentsByQuestionId') AS question,+ json_each(question.value) AS attachment+ WHERE item.thread_id = ${threadId}+ AND item.type = 'user_input_request'`,// A guest's waiting files are pending uploads that no message row holds yet.sql<{ id: string }>`@@ -5330,10 +5338,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =AND json_extract(message.value, '$.status') = 'pending'`,]).pipe(- Effect.map(([messages, renders, guestFiles]) => [+ Effect.map(([messages, renders, answerFiles, guestFiles]) => [...new Set([...messages.map((row) => row.id),...guestFiles.map((row) => row.id),+ ...answerFiles.map((row) => row.id),...threadHtmlRenderAttachmentIds(threadId,renders.map((row) => {diff --git a/apps/server/src/orchestration-v2/TurnStartReads.test.ts b/apps/server/src/orchestration-v2/TurnStartReads.test.tsindex caa941396a7..6db6b4c3088 100644--- a/apps/server/src/orchestration-v2/TurnStartReads.test.ts+++ b/apps/server/src/orchestration-v2/TurnStartReads.test.ts@@ -9,6 +9,7 @@ import {ProviderDriverKind,ProviderInstanceId,RunId,+ RuntimeRequestId,ThreadId,TurnItemId,type OrchestrationV2AppThread,@@ -360,6 +361,57 @@ it.effect.each(["sqlite"] as const)(yield* store.getTurnStartContext(ThreadId.make("missing"), runId).pipe(Effect.flip),ProjectionStore.ProjectionStoreThreadNotFoundError,);+ const file = {+ type: "file" as const,+ id: "message-file",+ name: "notes.pdf",+ mimeType: "application/pdf",+ sizeBytes: 12,+ };+ yield* store.apply({+ id: EventId.make("attachment:message-event"),+ type: "message.updated",+ threadId,+ runId,+ occurredAt: now,+ payload: { ...message, attachments: [file] },+ });+ yield* store.apply({+ id: EventId.make("attachment:question-event"),+ type: "turn-item.updated",+ threadId,+ runId: oldRunId,+ occurredAt: now,+ payload: {+ ...item,+ id: TurnItemId.make("attachment:question"),+ type: "user_input_request",+ requestId: RuntimeRequestId.make("attachment:request"),+ questions: [],+ questionAnswer: {+ requestId: "attachment:request",+ answers: { document: ["See document"], image: ["See image"] },+ attachmentsByQuestionId: {+ document: [{ ...file, id: "answer-document" }],+ image: [+ {+ type: "image",+ id: "answer-image",+ name: "photo.png",+ mimeType: "image/png",+ sizeBytes: 8,+ },+ ],+ },+ },+ },+ });+ assert.deepEqual((yield* store.getThreadAttachmentIds(threadId)).toSorted(), [+ "answer-document",+ "answer-image",+ "attachment:old",+ "message-file",+ ]);}).pipe(Effect.provide(layerDatabase)),);