Skip to content

Commit f56a477

Browse files
fix(server): thread deletion removes files attached to question answers (#26074)
Co-authored-by: maria-rcks <[email protected]>
1 parent 91c7c47 commit f56a477

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5319,6 +5319,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53195319
WHERE thread_id = ${threadId}
53205320
AND type = 'dynamic_tool'
53215321
AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`})
5322+
`,
5323+
sql<{ id: string }>`
5324+
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
5325+
FROM orchestration_v2_projection_turn_items AS item,
5326+
json_each(item.payload_json, '$.questionAnswer.attachmentsByQuestionId') AS question,
5327+
json_each(question.value) AS attachment
5328+
WHERE item.thread_id = ${threadId}
5329+
AND item.type = 'user_input_request'
53225330
`,
53235331
// A guest's waiting files are pending uploads that no message row holds yet.
53245332
sql<{ id: string }>`
@@ -5330,10 +5338,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53305338
AND json_extract(message.value, '$.status') = 'pending'
53315339
`,
53325340
]).pipe(
5333-
Effect.map(([messages, renders, guestFiles]) => [
5341+
Effect.map(([messages, renders, answerFiles, guestFiles]) => [
53345342
...new Set([
53355343
...messages.map((row) => row.id),
53365344
...guestFiles.map((row) => row.id),
5345+
...answerFiles.map((row) => row.id),
53375346
...threadHtmlRenderAttachmentIds(
53385347
threadId,
53395348
renders.map((row) => {

‎apps/server/src/orchestration-v2/TurnStartReads.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ProviderDriverKind,
1010
ProviderInstanceId,
1111
RunId,
12+
RuntimeRequestId,
1213
ThreadId,
1314
TurnItemId,
1415
type OrchestrationV2AppThread,
@@ -360,6 +361,57 @@ it.effect.each(["sqlite"] as const)(
360361
yield* store.getTurnStartContext(ThreadId.make("missing"), runId).pipe(Effect.flip),
361362
ProjectionStore.ProjectionStoreThreadNotFoundError,
362363
);
364+
const file = {
365+
type: "file" as const,
366+
id: "message-file",
367+
name: "notes.pdf",
368+
mimeType: "application/pdf",
369+
sizeBytes: 12,
370+
};
371+
yield* store.apply({
372+
id: EventId.make("attachment:message-event"),
373+
type: "message.updated",
374+
threadId,
375+
runId,
376+
occurredAt: now,
377+
payload: { ...message, attachments: [file] },
378+
});
379+
yield* store.apply({
380+
id: EventId.make("attachment:question-event"),
381+
type: "turn-item.updated",
382+
threadId,
383+
runId: oldRunId,
384+
occurredAt: now,
385+
payload: {
386+
...item,
387+
id: TurnItemId.make("attachment:question"),
388+
type: "user_input_request",
389+
requestId: RuntimeRequestId.make("attachment:request"),
390+
questions: [],
391+
questionAnswer: {
392+
requestId: "attachment:request",
393+
answers: { document: ["See document"], image: ["See image"] },
394+
attachmentsByQuestionId: {
395+
document: [{ ...file, id: "answer-document" }],
396+
image: [
397+
{
398+
type: "image",
399+
id: "answer-image",
400+
name: "photo.png",
401+
mimeType: "image/png",
402+
sizeBytes: 8,
403+
},
404+
],
405+
},
406+
},
407+
},
408+
});
409+
assert.deepEqual((yield* store.getThreadAttachmentIds(threadId)).toSorted(), [
410+
"answer-document",
411+
"answer-image",
412+
"attachment:old",
413+
"message-file",
414+
]);
363415
}).pipe(Effect.provide(layerDatabase)),
364416
);
365417

0 commit comments

Comments
 (0)