Skip to content

Commit 1dfb9e2

Browse files
committed
fix(server): thread deletion removes files attached to question answers
Deleting a thread leaves files attached to question answers on disk. Their metadata is stored on user input request items rather than message rows. Collect these attachment IDs in the SQLite and memory projection stores. Keep the existing deduplication. Tests: TurnStartReads.test.ts, loads startup state without obsolete transcript payloads in sqlite and memory. The regression includes two question-answer attachments and an ordinary message attachment.
1 parent 7873f53 commit 1dfb9e2

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5324,6 +5324,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53245324
WHERE thread_id = ${threadId}
53255325
AND type = 'dynamic_tool'
53265326
AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`})
5327+
`,
5328+
sql<{ id: string }>`
5329+
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
5330+
FROM orchestration_v2_projection_turn_items AS item,
5331+
json_each(item.payload_json, '$.questionAnswer.attachmentsByQuestionId') AS question,
5332+
json_each(question.value) AS attachment
5333+
WHERE item.thread_id = ${threadId}
5334+
AND item.type = 'user_input_request'
53275335
`,
53285336
// A guest's waiting files are pending uploads that no message row holds yet.
53295337
sql<{ id: string }>`
@@ -5335,10 +5343,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53355343
AND json_extract(message.value, '$.status') = 'pending'
53365344
`,
53375345
]).pipe(
5338-
Effect.map(([messages, renders, guestFiles]) => [
5346+
Effect.map(([messages, renders, answerFiles, guestFiles]) => [
53395347
...new Set([
53405348
...messages.map((row) => row.id),
53415349
...guestFiles.map((row) => row.id),
5350+
...answerFiles.map((row) => row.id),
53425351
...threadHtmlRenderAttachmentIds(
53435352
threadId,
53445353
renders.map((row) => {
@@ -7197,6 +7206,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
71977206
...projection.messages.flatMap((message) =>
71987207
message.attachments.map((attachment) => attachment.id),
71997208
),
7209+
...projection.turnItems.flatMap((item) =>
7210+
item.type === "user_input_request" && item.questionAnswer
7211+
? Object.values(item.questionAnswer.attachmentsByQuestionId)
7212+
.flat()
7213+
.map((attachment) => attachment.id)
7214+
: [],
7215+
),
72007216
...(projection.thread.guestShare?.messages ?? []).flatMap((message) =>
72017217
message.status === "pending"
72027218
? (message.attachments ?? []).map((attachment) => attachment.id)

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

Lines changed: 53 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,58 @@ it.effect.each(["sqlite", "memory"] 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+
// Thread deletion must collect answer files even when no message holds their ids.
410+
assert.deepEqual(
411+
(yield* store.getThreadAttachmentIds(threadId))
412+
.filter((id) => id !== "attachment:old")
413+
.sort(),
414+
["answer-document", "answer-image", "message-file"],
415+
);
363416
}).pipe(
364417
Effect.provide(
365418
storage === "sqlite"

0 commit comments

Comments
 (0)