The best way to control Claude Code, Codex and more from any computer or phone
fix(server): thread deletion removes files attached to question answers (#26074)
Co-authored-by: maria-rcks <maria@kuuro.net>
Fuksss committed
Oct 30, 2026 at 22:18 UTC
f56a4775fd9e97b6f8afa47804b4cab27228601e
2 files changed
+62
-1
apps/server/src/orchestration-v2/ProjectionStore.ts
+10
-1
@@ -5319,6 +5319,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5319
WHERE thread_id = ${threadId}
5320
AND type = 'dynamic_tool'
5321
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'
5330
`,
5331
// A guest's waiting files are pending uploads that no message row holds yet.
5332
sql<{ id: string }>`
@@ -5330,10 +5338,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5338
AND json_extract(message.value, '$.status') = 'pending'
5339
`,
5340
]).pipe(
5333
- Effect.map(([messages, renders, guestFiles]) => [
5341
+ Effect.map(([messages, renders, answerFiles, guestFiles]) => [
5342
...new Set([
5343
...messages.map((row) => row.id),
5344
...guestFiles.map((row) => row.id),
5345
+ ...answerFiles.map((row) => row.id),
5346
...threadHtmlRenderAttachmentIds(
5347
threadId,
5348
renders.map((row) => {
apps/server/src/orchestration-v2/TurnStartReads.test.ts
+52
@@ -9,6 +9,7 @@ import {
9
ProviderDriverKind,
10
ProviderInstanceId,
11
RunId,
12
+ RuntimeRequestId,
13
ThreadId,
14
TurnItemId,
15
type OrchestrationV2AppThread,
@@ -360,6 +361,57 @@ it.effect.each(["sqlite"] as const)(
361
yield* store.getTurnStartContext(ThreadId.make("missing"), runId).pipe(Effect.flip),
362
ProjectionStore.ProjectionStoreThreadNotFoundError,
363
);
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
+ ]);
415
}).pipe(Effect.provide(layerDatabase)),
416
);
417