TypeScript
fix(server): deleting a thread also removes its agent preview recordings (#29853)
Alex Southwell2863eded441c
diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.tsindex 616b0d90c17..a2b531e3fa5 100644--- a/apps/server/src/attachmentStore.ts+++ b/apps/server/src/attachmentStore.ts@@ -4,7 +4,11 @@ import * as NodeFS from "node:fs";import * as NodePath from "node:path";import type { ChatAttachment } from "@t3tools/contracts";-import { htmlRenderFromToolItem, mcpAppFromToolItem } from "@t3tools/shared/toolOutput";+import {+ htmlRenderFromToolItem,+ mcpAppFromToolItem,+ previewRecordingIdFromToolItem,+} from "@t3tools/shared/toolOutput";import {normalizeAttachmentRelativePath,@@ -133,7 +137,9 @@ export function threadHtmlRenderAttachmentIds(if (segment === null) return [];return Array.from(items).flatMap((item) => {const attachmentId =- htmlRenderFromToolItem(item)?.attachmentId ?? mcpAppFromToolItem(item)?.attachmentId;+ htmlRenderFromToolItem(item)?.attachmentId ??+ mcpAppFromToolItem(item)?.attachmentId ??+ previewRecordingIdFromToolItem(item);return attachmentId !== undefined &&parseThreadSegmentFromAttachmentId(attachmentId) === segment? [attachmentId]diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.tsindex 9696286f7b4..a46c8017731 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts@@ -5504,7 +5504,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =FROM orchestration_v2_projection_turn_itemsWHERE thread_id = ${threadId}AND type = 'dynamic_tool'- AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`})+ AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}+ OR payload_json LIKE '%preview_recording_stop%')`,sql<{ id: string }>`SELECT DISTINCT json_extract(attachment.value, '$.id') AS iddiff --git a/apps/server/src/orchestration-v2/TurnStartReads.test.ts b/apps/server/src/orchestration-v2/TurnStartReads.test.tsindex 6db6b4c3088..cfc362c9db0 100644--- a/apps/server/src/orchestration-v2/TurnStartReads.test.ts+++ b/apps/server/src/orchestration-v2/TurnStartReads.test.ts@@ -406,11 +406,30 @@ it.effect.each(["sqlite"] as const)(},},});+ const recordingId = "thread-completion-reads-0b9f6c1e-3c1d-4f7a-9d2e-5a6b7c8d9e0f-webm";+ yield* store.apply({+ id: EventId.make("attachment:recording-event"),+ type: "turn-item.updated",+ threadId,+ runId: oldRunId,+ occurredAt: now,+ payload: {+ ...item,+ id: TurnItemId.make("attachment:recording"),+ type: "dynamic_tool",+ toolName: "mcp__t3-code__preview_recording_stop",+ input: {},+ output: [+ { type: "text", text: JSON.stringify({ id: recordingId, mimeType: "video/webm" }) },+ ],+ },+ });assert.deepEqual((yield* store.getThreadAttachmentIds(threadId)).toSorted(), ["answer-document","answer-image","attachment:old","message-file",+ recordingId,]);}).pipe(Effect.provide(layerDatabase)),);diff --git a/packages/shared/src/toolOutput.ts b/packages/shared/src/toolOutput.tsindex 05bb49fc6a5..b3a88e59de1 100644--- a/packages/shared/src/toolOutput.ts+++ b/packages/shared/src/toolOutput.ts@@ -191,6 +191,21 @@ export function htmlRenderFromToolItem(item: {return output?.isError ? undefined : output?.htmlRender;}+/** The thread-owned file `preview_recording_stop` stored for the agent. */+export function previewRecordingIdFromToolItem(item: {+ readonly toolName: string | null | undefined;+ readonly output?: unknown;+}): string | undefined {+ if (resolveT3McpToolId(item.toolName) !== "preview_recording_stop") return undefined;+ const budget: ResultReadBudget = {+ remainingBytes: MAX_PARSED_BYTES,+ remainingNodes: MAX_ENVELOPE_NODES,+ exceeded: false,+ };+ const result = readResult(item.output, budget);+ return result.failed || budget.exceeded ? undefined : boundedId(result.data?.id);+}+/** Accept app references only when server and tool match the item, so tools cannot impersonate another app source. */export function mcpAppFromToolItem(item: {readonly toolName: string | null | undefined;