From c49f1a27576be13d0ce3a6aa4469f8fc7b912456 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 07:07:38 +0000 Subject: [PATCH 1/5] test(server): served tool-output images stay out of both SQLite tables Failing reproduction for #26591: a served image block (such as the one a Claude Read returns) is stored inline as base64 in orchestration_events and orchestration_v2_projection_turn_items. AI-assisted; verified with the tests and tools listed above. --- .../orchestration-v2/ProviderEventIngestor.test.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 520fea7c0575b..52efdae43b15b 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -37,7 +37,6 @@ import * as SqlClient from "effect/sql/SqlClient"; import * as SqlError from "effect/sql/SqlError"; import * as Stream from "effect/Stream"; -import { toolOutputImages } from "@t3tools/shared/toolOutput"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ServerConfig from "../config.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; @@ -1551,7 +1550,7 @@ layer("ProviderEventIngestorV2", (it) => { }), ); - it.effect("stores tool image bytes only where a tool-output-image asset serves them", () => + it.effect("keeps tool image bytes out of both SQLite tables, served ones included", () => Effect.gen(function* () { const now = yield* DateTime.now; const eventSink = yield* EventSink.EventSinkV2; @@ -1628,17 +1627,12 @@ layer("ProviderEventIngestorV2", (it) => { }); assert.equal(storedJson.includes(readBase64), false); - assert.equal(storedJson.includes(screenshotBase64), true); + assert.equal(storedJson.includes(screenshotBase64), false); assert.deepEqual(projectedRead?.type === "dynamic_tool" ? projectedRead.output : null, { type: "image", file: { type: "image/png", originalSize: 30_000, sizeBytes: 30_000 }, }); - assert.deepEqual( - toolOutputImages( - projectedScreenshot?.type === "dynamic_tool" ? projectedScreenshot.output : null, - ), - [{ mimeType: "image/png", data: screenshotBase64 }], - ); + assert.equal(JSON.stringify(projectedScreenshot).includes(screenshotBase64), false); }), ); From f39297ef68c303e340e5b467c968d28388860c4c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 07:16:56 +0000 Subject: [PATCH 2/5] fix(server): tool output images are stored once on disk, not inline in SQLite Images a tool returns (such as Claude's Read on a screenshot) were kept inline as base64 in both the turn-item.updated event and the projected turn item, because the tool-output-image asset served them from that row. The ingestor now writes each served image to the attachment store under a deterministic per-thread id and keeps only { type, mimeType, attachmentId } in the row. AssetAccess serves the file by that id, and thread deletion removes it with the thread's other attachments. Older rows with inline bytes are still served as before. AI-assisted; verified with the tests and tools listed above. Fixes pingdotgg/t3code#26591 --- apps/server/src/assets/AssetAccess.test.ts | 49 ++++++++---- apps/server/src/assets/AssetAccess.ts | 10 +++ apps/server/src/attachmentStore.ts | 17 ++++ .../src/orchestration-v2/ProjectionStore.ts | 42 +++++----- .../ProviderEventIngestor.test.ts | 13 ++- .../orchestration-v2/ProviderEventIngestor.ts | 20 ++++- .../ProviderSessionManager.test.ts | 2 + .../orchestration-v2/TurnStartReads.test.ts | 17 ++++ .../testkit/ProviderReplayHarness.ts | 4 +- .../orchestration-v2/toolOutputImageBytes.ts | 80 +++++++++++++++++++ packages/shared/src/toolOutput.ts | 11 ++- 11 files changed, 224 insertions(+), 41 deletions(-) diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts index d762a15a830f8..ff8cbf7cce2a4 100644 --- a/apps/server/src/assets/AssetAccess.test.ts +++ b/apps/server/src/assets/AssetAccess.test.ts @@ -102,16 +102,23 @@ const oversizedScreenshotItem = { }, }; +const storedScreenshotAttachmentId = "thread-1-00000000-0000-4000-8000-000000000041-png"; +const storedScreenshotItem = { + ...screenshotItem, + id: TurnItemId.make("tool-screenshot-stored"), + output: { + content: [{ type: "image", mimeType: "image/png", attachmentId: storedScreenshotAttachmentId }], + }, +}; + const layerTest = Layer.mergeAll( NodeHttpPlatform.layer, Layer.mock(Orchestrator.OrchestratorV2)({ getTurnItem: ({ itemId }) => Effect.succeed( - itemId === screenshotItem.id - ? screenshotItem - : itemId === oversizedScreenshotItem.id - ? oversizedScreenshotItem - : null, + [screenshotItem, oversizedScreenshotItem, storedScreenshotItem].find( + (item) => item.id === itemId, + ) ?? null, ), }), layerConfig, @@ -186,23 +193,35 @@ describe("AssetAccess", () => { ); }); - it.effect("serves an image a tool returned inline from the stored item", () => + it.effect("serves an image a tool returned from the stored item or its attachment file", () => Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* fs.makeDirectory(config.attachmentsDir, { recursive: true }); + yield* fs.writeFile( + path.join(config.attachmentsDir, `${storedScreenshotAttachmentId}.png`), + screenshotPng, + ); const resource = { _tag: "tool-output-image" as const, threadId: screenshotItem.threadId, itemId: screenshotItem.id, index: 0, }; - const result = yield* issueAssetUrl({ resource }); - expect(result.imageDimensions).toEqual({ width: 390, height: 844 }); - const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); - const separator = suffix.indexOf("/"); - expect(yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1))).toEqual({ - kind: "bytes", - mimeType: "image/png", - bytes: Buffer.from(screenshotPng), - }); + for (const itemId of [screenshotItem.id, storedScreenshotItem.id]) { + const result = yield* issueAssetUrl({ resource: { ...resource, itemId } }); + expect(result.imageDimensions).toEqual({ width: 390, height: 844 }); + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); + const separator = suffix.indexOf("/"); + expect( + yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1)), + ).toEqual({ + kind: "bytes", + mimeType: "image/png", + bytes: Buffer.from(screenshotPng), + }); + } const missing = yield* issueAssetUrl({ resource: { ...resource, index: 1 } }).pipe( Effect.flip, ); diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts index ee211b2bdc36d..cffedb8760697 100644 --- a/apps/server/src/assets/AssetAccess.ts +++ b/apps/server/src/assets/AssetAccess.ts @@ -243,6 +243,16 @@ const readToolOutputImage = Effect.fn("AssetAccess.readToolOutputImage")(functio const item = yield* orchestrator.getTurnItem(input); const image = item?.type === "dynamic_tool" ? toolOutputImages(item.output)[input.index] : undefined; + if (image?.attachmentId !== undefined) { + const config = yield* ServerConfig.ServerConfig; + const filePath = resolveAttachmentPathById({ + attachmentsDir: config.attachmentsDir, + attachmentId: image.attachmentId, + }); + if (filePath === null) return null; + const fileSystem = yield* FileSystem.FileSystem; + return { mimeType: image.mimeType, bytes: yield* fileSystem.readFile(filePath) }; + } return image?.data === undefined || image.data.length > MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH ? null : { mimeType: image.mimeType, bytes: Buffer.from(image.data, "base64") }; diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.ts index a2b531e3fa5b0..0ce12c4a00284 100644 --- a/apps/server/src/attachmentStore.ts +++ b/apps/server/src/attachmentStore.ts @@ -8,6 +8,7 @@ import { htmlRenderFromToolItem, mcpAppFromToolItem, previewRecordingIdFromToolItem, + toolOutputImages, } from "@t3tools/shared/toolOutput"; import { @@ -147,6 +148,22 @@ export function threadHtmlRenderAttachmentIds( }); } +/** Like threadHtmlRenderAttachmentIds, for the tool output images the server stored. */ +export function threadToolOutputImageAttachmentIds( + threadId: string, + items: Iterable<{ readonly output?: unknown }>, +) { + const segment = toSafeThreadAttachmentSegment(threadId); + if (segment === null) return []; + return Array.from(items).flatMap((item) => + toolOutputImages(item.output).flatMap(({ attachmentId }) => + attachmentId !== undefined && parseThreadSegmentFromAttachmentId(attachmentId) === segment + ? [attachmentId] + : [], + ), + ); +} + export function attachmentRelativePath(attachment: ChatAttachment): string | null { switch (attachment.type) { case "image": { diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index d869eb9890c56..348200cc78903 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -95,7 +95,10 @@ import * as SqlClient from "effect/sql/SqlClient"; import type * as Statement from "effect/sql/Statement"; import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp"; -import { threadHtmlRenderAttachmentIds } from "../attachmentStore.ts"; +import { + threadHtmlRenderAttachmentIds, + threadToolOutputImageAttachmentIds, +} from "../attachmentStore.ts"; import { readableSearchText } from "./messageSearchIndex.ts"; import { isThreadHistoryUserTurn, @@ -5859,7 +5862,7 @@ export const layer: Layer.Layer = WHERE thread_id = ${threadId} AND type = 'dynamic_tool' AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`} - OR payload_json LIKE '%preview_recording_stop%') + OR payload_json LIKE '%preview_recording_stop%' OR payload_json LIKE '%attachmentId%') `, sql<{ id: string }>` SELECT DISTINCT json_extract(attachment.value, '$.id') AS id @@ -5879,23 +5882,24 @@ export const layer: Layer.Layer = AND json_extract(message.value, '$.status') = 'pending' `, ]).pipe( - 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) => { - const item = parseEncodedPayload(row.payload_json); - return { - toolName: typeof item.toolName === "string" ? item.toolName : null, - output: item.output, - }; - }), - ), - ]), - ]), + Effect.map(([messages, renders, answerFiles, guestFiles]) => { + const renderItems = renders.map((row) => { + const item = parseEncodedPayload(row.payload_json); + return { + toolName: typeof item.toolName === "string" ? item.toolName : null, + output: item.output, + }; + }); + return [ + ...new Set([ + ...messages.map((row) => row.id), + ...guestFiles.map((row) => row.id), + ...answerFiles.map((row) => row.id), + ...threadHtmlRenderAttachmentIds(threadId, renderItems), + ...threadToolOutputImageAttachmentIds(threadId, renderItems), + ]), + ]; + }), Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })), ); diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 52efdae43b15b..2be134e0f0126 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -37,6 +37,7 @@ import * as SqlClient from "effect/sql/SqlClient"; import * as SqlError from "effect/sql/SqlError"; import * as Stream from "effect/Stream"; +import { toolOutputImages } from "@t3tools/shared/toolOutput"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ServerConfig from "../config.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; @@ -68,7 +69,7 @@ const layerTestEventSink = EventSink.layer.pipe( ); const layerGit = GitVcsDriver.layer.pipe( - Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "ingestor-git-" })), + Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "ingestor-git-" })), Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); @@ -1633,6 +1634,16 @@ layer("ProviderEventIngestorV2", (it) => { file: { type: "image/png", originalSize: 30_000, sizeBytes: 30_000 }, }); assert.equal(JSON.stringify(projectedScreenshot).includes(screenshotBase64), false); + const [stored] = toolOutputImages( + projectedScreenshot?.type === "dynamic_tool" ? projectedScreenshot.output : null, + ); + assert.equal(stored?.mimeType, "image/png"); + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const storedBytes = yield* fs.readFile( + `${config.attachmentsDir}/${stored?.attachmentId ?? "missing"}.png`, + ); + assert.equal(Buffer.from(storedBytes).toString("base64"), screenshotBase64); }), ); diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 623fddb491108..7f5906be70e56 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -20,7 +20,9 @@ import { import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as SqlError from "effect/sql/SqlError"; @@ -31,6 +33,7 @@ import { ProviderTurnAnalytics, type ProviderTurnAnalyticsContext, } from "../telemetry/ProviderUsageAnalytics.ts"; +import * as ServerConfig from "../config.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; import * as EventSink from "./EventSink.ts"; @@ -39,7 +42,10 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import { makeProviderFailureTurnItem } from "@t3tools/provider-core/server/failure"; import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts"; -import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts"; +import { + storeServedToolOutputImages, + stripUnservedToolOutputImageBytes, +} from "./toolOutputImageBytes.ts"; import { boundFileChangeForStorage } from "./WireProjection.ts"; export class ProviderEventNormalizeError extends Schema.TaggedError()( @@ -244,6 +250,9 @@ export const layer: Layer.Layer< | ProjectionStore.ProjectionStoreV2 | ThreadCommandExecutor.ThreadCommandExecutor | ProviderInstanceRegistry.ProviderInstanceRegistry + | ServerConfig.ServerConfig + | FileSystem.FileSystem + | Path.Path > = Layer.effect( ProviderEventIngestorV2, Effect.gen(function* () { @@ -255,6 +264,9 @@ export const layer: Layer.Layer< const analytics = yield* ProviderTurnAnalytics; const agentScope = yield* AgentScope; const instances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; + const imageStoreContext = yield* Effect.context< + ServerConfig.ServerConfig | FileSystem.FileSystem | Path.Path + >(); const scope = yield* Effect.scope; const completedTurnAnalytics = new Set(); @@ -575,7 +587,11 @@ export const layer: Layer.Layer< type: "turn-item.updated", threadId: input.event.turnItem.threadId, payload: boundFileChangeForStorage( - stripUnservedToolOutputImageBytes(input.event.turnItem), + stripUnservedToolOutputImageBytes( + yield* storeServedToolOutputImages(input.event.turnItem).pipe( + Effect.provide(imageStoreContext), + ), + ), ), runId: input.event.turnItem.runId, nodeId: input.event.turnItem.nodeId, diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index 5e494b8b31904..26bf31a31993f 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -35,6 +35,7 @@ import { TestClock } from "effect/testing"; import { HttpServer } from "effect/http"; import { ProviderWorkspaceMissingError } from "../provider/Errors.ts"; +import * as ServerConfig from "../config.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as ProjectService from "../project/ProjectService.ts"; import * as McpProviderSession from "@t3tools/provider-core/server/mcpSession"; @@ -680,6 +681,7 @@ function layerTest(input: { Layer.mock(GitVcsDriver.GitVcsDriver)({ resolveBranchName: () => Effect.succeed(null), }), + ServerConfig.layerTest(process.cwd(), { prefix: "provider-session-manager-" }), ), ), ); diff --git a/apps/server/src/orchestration-v2/TurnStartReads.test.ts b/apps/server/src/orchestration-v2/TurnStartReads.test.ts index cfc362c9db080..703c64f6b285e 100644 --- a/apps/server/src/orchestration-v2/TurnStartReads.test.ts +++ b/apps/server/src/orchestration-v2/TurnStartReads.test.ts @@ -424,12 +424,29 @@ it.effect.each(["sqlite"] as const)( ], }, }); + const toolImageId = "thread-completion-reads-1c2d3e4f-5a6b-4c7d-8e9f-0a1b2c3d4e5f-png"; + yield* store.apply({ + id: EventId.make("attachment:tool-image-event"), + type: "turn-item.updated", + threadId, + runId: oldRunId, + occurredAt: now, + payload: { + ...item, + id: TurnItemId.make("attachment:tool-image"), + type: "dynamic_tool", + toolName: "Read", + input: {}, + output: [{ type: "image", mimeType: "image/png", attachmentId: toolImageId }], + }, + }); assert.deepEqual((yield* store.getThreadAttachmentIds(threadId)).toSorted(), [ "answer-document", "answer-image", "attachment:old", "message-file", recordingId, + toolImageId, ]); }).pipe(Effect.provide(layerDatabase)), ); diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts index e3672cfcc1c92..d85ca5d242cfc 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts @@ -367,8 +367,8 @@ export function layerWithRegistry( IdAllocator.layer, ThreadCommandExecutor.layer, GitVcsDriver.layer.pipe( - Layer.provide(layerServerConfig), - Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + Layer.provideMerge(layerServerConfig), + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ), ), ), diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts index 6cde6c5448c71..d652f8e6122b6 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts @@ -1,10 +1,20 @@ import type { OrchestrationV2TurnItem } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Base64 from "effect/encoding/Base64"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; +import * as Predicate from "effect/Predicate"; +import * as Result from "effect/Result"; import { MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH, readToolOutputImage, toolOutputImageBlocks, } from "@t3tools/shared/toolOutput"; +import { chatAttachmentStableKey, createDeterministicAttachmentId } from "../attachmentStore.ts"; +import * as ServerConfig from "../config.ts"; +import { inferImageExtension } from "../imageMime.ts"; + const IMAGE_MIME_KEYS = ["mimeType", "mime_type", "media_type", "type"] as const; const IMAGE_BODY_KEYS = new Set(["data", "blob", "base64"]); const MAX_DEPTH = 32; @@ -112,3 +122,73 @@ export function stripUnservedToolOutputImageBytes( const output = strip(item.output, undefined, { output: item.output, served: undefined }, 0); return output === item.output ? item : { ...item, output }; } + +function mapOutputBlocks(value: unknown, map: (block: unknown) => unknown): unknown { + if (Array.isArray(value)) return value.map(map); + if (Predicate.isObject(value) && Array.isArray(value.content)) { + return { ...value, content: value.content.map(map) }; + } + return map(value); +} + +/** Moves served image bytes to the attachment store so SQLite rows keep only a reference. + * Ids are deterministic per item, index, and bytes, so repeated updates reuse one file. */ +export const storeServedToolOutputImages = Effect.fn( + "ToolOutputImageBytes.storeServedToolOutputImages", +)(function* (item: OrchestrationV2TurnItem) { + if (item.type !== "dynamic_tool") return item; + const blocks = toolOutputImageBlocks(item.output); + if (blocks.length === 0) return item; + const config = yield* ServerConfig.ServerConfig; + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const replacements = new Map(); + yield* Effect.forEach( + blocks, + Effect.fnUntraced(function* (block, index) { + const image = readToolOutputImage(block); + if (image === null) return; + const reference = { type: "image", mimeType: image.mimeType }; + // Provider output must not point at another thread's stored file. + if (image.data === undefined || image.data.length > MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH) { + if (image.attachmentId !== undefined) replacements.set(block, reference); + return; + } + const decoded = Base64.decode(image.data); + if (Result.isFailure(decoded)) return; + const bytes = decoded.success; + const baseId = createDeterministicAttachmentId( + item.threadId, + chatAttachmentStableKey(item.id, index, bytes), + ); + if (baseId === null) return; + const extension = inferImageExtension({ mimeType: image.mimeType }); + const attachmentId = `${baseId}-${extension.slice(1)}`; + const filePath = path.join(config.attachmentsDir, `${attachmentId}${extension}`); + const stored = yield* fileSystem.exists(filePath).pipe( + Effect.flatMap((exists) => + exists + ? Effect.void + : fileSystem + .makeDirectory(config.attachmentsDir, { recursive: true }) + .pipe(Effect.andThen(fileSystem.writeFile(filePath, bytes))), + ), + Effect.as(true), + Effect.catch((cause) => + Effect.logWarning("Keeping tool output image inline; storing it failed.", { + threadId: item.threadId, + itemId: item.id, + cause, + }).pipe(Effect.as(false)), + ), + ); + if (stored) replacements.set(block, { ...reference, attachmentId }); + }), + { concurrency: 1, discard: true }, + ); + if (replacements.size === 0) return item; + return { + ...item, + output: mapOutputBlocks(item.output, (block) => replacements.get(block) ?? block), + }; +}); diff --git a/packages/shared/src/toolOutput.ts b/packages/shared/src/toolOutput.ts index 6287c673ef639..b787a50caca18 100644 --- a/packages/shared/src/toolOutput.ts +++ b/packages/shared/src/toolOutput.ts @@ -241,6 +241,8 @@ export function toolOutputIndicatesFailure(text: string): boolean { export interface ToolOutputImage { readonly mimeType: string; readonly data?: string; + /** Set by the server once it moves the bytes to its attachment store. */ + readonly attachmentId?: string; } /** Raster types only, so agent SVG/HTML cannot be served inline. */ @@ -254,7 +256,10 @@ export function readToolOutputImage(block: unknown): ToolOutputImage | null { return null; } const image = { mimeType: mimeType.toLowerCase() }; - return typeof data === "string" ? { ...image, data } : image; + if (typeof data === "string") return { ...image, data }; + return source === undefined && typeof block.attachmentId === "string" + ? { ...image, attachmentId: block.attachmentId } + : image; } function outputBlocks(value: unknown): ReadonlyArray { @@ -296,7 +301,9 @@ export function mcpToolResultOutput(result: { export function omitToolOutputImageData(value: unknown): unknown { const omit = (block: unknown) => { const image = readToolOutputImage(block); - return image?.data === undefined ? block : { type: "image", mimeType: image.mimeType }; + return image?.data === undefined && image?.attachmentId === undefined + ? block + : { type: "image", mimeType: image.mimeType }; }; if (Array.isArray(value)) return value.map(omit); if (Predicate.isObject(value) && Array.isArray(value.content)) { From d684bca3dad0b420975e86adf8182854846feffb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 07:40:35 +0000 Subject: [PATCH 3/5] fix(server): stored tool images survive fork deletion and failed writes Deleting a thread no longer schedules its stored tool-output images for cleanup: forks inherit those items, and owning the id segment does not prove nothing else references the file. The files now stay on disk after deletion, as the inline bytes stayed in SQLite before. Image files are written to a unique .part path and renamed into place, so a write that fails partway never leaves a file that a later update would accept as complete. Stale .part files are reclaimed by the existing sweep. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/attachmentStore.ts | 17 ----- .../src/orchestration-v2/ProjectionStore.ts | 42 ++++++------- .../orchestration-v2/TurnStartReads.test.ts | 17 ----- .../toolOutputImageBytes.test.ts | 62 ++++++++++++++++++- .../orchestration-v2/toolOutputImageBytes.ts | 15 ++++- 5 files changed, 92 insertions(+), 61 deletions(-) diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.ts index 0ce12c4a00284..a2b531e3fa5b0 100644 --- a/apps/server/src/attachmentStore.ts +++ b/apps/server/src/attachmentStore.ts @@ -8,7 +8,6 @@ import { htmlRenderFromToolItem, mcpAppFromToolItem, previewRecordingIdFromToolItem, - toolOutputImages, } from "@t3tools/shared/toolOutput"; import { @@ -148,22 +147,6 @@ export function threadHtmlRenderAttachmentIds( }); } -/** Like threadHtmlRenderAttachmentIds, for the tool output images the server stored. */ -export function threadToolOutputImageAttachmentIds( - threadId: string, - items: Iterable<{ readonly output?: unknown }>, -) { - const segment = toSafeThreadAttachmentSegment(threadId); - if (segment === null) return []; - return Array.from(items).flatMap((item) => - toolOutputImages(item.output).flatMap(({ attachmentId }) => - attachmentId !== undefined && parseThreadSegmentFromAttachmentId(attachmentId) === segment - ? [attachmentId] - : [], - ), - ); -} - export function attachmentRelativePath(attachment: ChatAttachment): string | null { switch (attachment.type) { case "image": { diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 348200cc78903..d869eb9890c56 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -95,10 +95,7 @@ import * as SqlClient from "effect/sql/SqlClient"; import type * as Statement from "effect/sql/Statement"; import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp"; -import { - threadHtmlRenderAttachmentIds, - threadToolOutputImageAttachmentIds, -} from "../attachmentStore.ts"; +import { threadHtmlRenderAttachmentIds } from "../attachmentStore.ts"; import { readableSearchText } from "./messageSearchIndex.ts"; import { isThreadHistoryUserTurn, @@ -5862,7 +5859,7 @@ export const layer: Layer.Layer = WHERE thread_id = ${threadId} AND type = 'dynamic_tool' AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`} - OR payload_json LIKE '%preview_recording_stop%' OR payload_json LIKE '%attachmentId%') + OR payload_json LIKE '%preview_recording_stop%') `, sql<{ id: string }>` SELECT DISTINCT json_extract(attachment.value, '$.id') AS id @@ -5882,24 +5879,23 @@ export const layer: Layer.Layer = AND json_extract(message.value, '$.status') = 'pending' `, ]).pipe( - Effect.map(([messages, renders, answerFiles, guestFiles]) => { - const renderItems = renders.map((row) => { - const item = parseEncodedPayload(row.payload_json); - return { - toolName: typeof item.toolName === "string" ? item.toolName : null, - output: item.output, - }; - }); - return [ - ...new Set([ - ...messages.map((row) => row.id), - ...guestFiles.map((row) => row.id), - ...answerFiles.map((row) => row.id), - ...threadHtmlRenderAttachmentIds(threadId, renderItems), - ...threadToolOutputImageAttachmentIds(threadId, renderItems), - ]), - ]; - }), + 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) => { + const item = parseEncodedPayload(row.payload_json); + return { + toolName: typeof item.toolName === "string" ? item.toolName : null, + output: item.output, + }; + }), + ), + ]), + ]), Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })), ); diff --git a/apps/server/src/orchestration-v2/TurnStartReads.test.ts b/apps/server/src/orchestration-v2/TurnStartReads.test.ts index 703c64f6b285e..cfc362c9db080 100644 --- a/apps/server/src/orchestration-v2/TurnStartReads.test.ts +++ b/apps/server/src/orchestration-v2/TurnStartReads.test.ts @@ -424,29 +424,12 @@ it.effect.each(["sqlite"] as const)( ], }, }); - const toolImageId = "thread-completion-reads-1c2d3e4f-5a6b-4c7d-8e9f-0a1b2c3d4e5f-png"; - yield* store.apply({ - id: EventId.make("attachment:tool-image-event"), - type: "turn-item.updated", - threadId, - runId: oldRunId, - occurredAt: now, - payload: { - ...item, - id: TurnItemId.make("attachment:tool-image"), - type: "dynamic_tool", - toolName: "Read", - input: {}, - output: [{ type: "image", mimeType: "image/png", attachmentId: toolImageId }], - }, - }); assert.deepEqual((yield* store.getThreadAttachmentIds(threadId)).toSorted(), [ "answer-document", "answer-image", "attachment:old", "message-file", recordingId, - toolImageId, ]); }).pipe(Effect.provide(layerDatabase)), ); diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts index 4a885dd4cf031..dc97908aeacd0 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts @@ -4,10 +4,20 @@ import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages, } from "@t3tools/shared/toolOutput"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { it as effectIt } from "@effect/vitest"; import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as PlatformError from "effect/PlatformError"; import { describe, expect, it } from "vite-plus/test"; -import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts"; +import * as ServerConfig from "../config.ts"; +import { + storeServedToolOutputImages, + stripUnservedToolOutputImageBytes, +} from "./toolOutputImageBytes.ts"; const PNG_BYTES = 3_000; const PNG_BASE64 = Buffer.alloc(PNG_BYTES, 7).toString("base64"); @@ -213,3 +223,53 @@ describe("stripUnservedToolOutputImageBytes", () => { expect(stripped.result[0]).toBe(untouched); }); }); + +describe("storeServedToolOutputImages", () => { + effectIt.effect("keeps an image inline until a retried write stores all of its bytes", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let failNextWrite = true; + const faultyFs = { + ...fs, + writeFile: (filePath: string, content: Uint8Array) => + failNextWrite + ? fs.writeFile(filePath, content.subarray(0, 10)).pipe( + Effect.andThen( + Effect.fail( + PlatformError.systemError({ + _tag: "Unknown", + module: "FileSystem", + method: "writeFile", + description: "ENOSPC", + }), + ), + ), + ) + : fs.writeFile(filePath, content), + } satisfies FileSystem.FileSystem; + const item = toolItem({ + content: [ + { type: "image", source: { type: "base64", media_type: "image/png", data: PNG_BASE64 } }, + ], + }); + const store = storeServedToolOutputImages(item).pipe( + Effect.provideService(FileSystem.FileSystem, faultyFs), + ); + + expect(yield* store).toBe(item); + failNextWrite = false; + const stored = yield* store; + const [image] = toolOutputImages(stored.type === "dynamic_tool" ? stored.output : null); + const config = yield* ServerConfig.ServerConfig; + const bytes = yield* fs.readFile(`${config.attachmentsDir}/${image?.attachmentId}.png`); + expect(Buffer.from(bytes).toString("base64")).toBe(PNG_BASE64); + expect((yield* fs.readDirectory(config.attachmentsDir)).length).toBe(1); + }).pipe( + Effect.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "tool-output-images-" }).pipe( + Layer.provideMerge(NodeServices.layer), + ), + ), + ), + ); +}); diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts index d652f8e6122b6..4f2a3db8d4e81 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts @@ -1,3 +1,6 @@ +// @effect-diagnostics nodeBuiltinImport:off +import * as NodeCrypto from "node:crypto"; + import type { OrchestrationV2TurnItem } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Base64 from "effect/encoding/Base64"; @@ -165,13 +168,19 @@ export const storeServedToolOutputImages = Effect.fn( const extension = inferImageExtension({ mimeType: image.mimeType }); const attachmentId = `${baseId}-${extension.slice(1)}`; const filePath = path.join(config.attachmentsDir, `${attachmentId}${extension}`); + const partPath = `${filePath}.${NodeCrypto.randomUUID()}.part`; const stored = yield* fileSystem.exists(filePath).pipe( Effect.flatMap((exists) => exists ? Effect.void - : fileSystem - .makeDirectory(config.attachmentsDir, { recursive: true }) - .pipe(Effect.andThen(fileSystem.writeFile(filePath, bytes))), + : fileSystem.makeDirectory(config.attachmentsDir, { recursive: true }).pipe( + // Rename so an interrupted write never leaves a file that passes the exists check. + Effect.andThen(fileSystem.writeFile(partPath, bytes)), + Effect.andThen(fileSystem.rename(partPath, filePath)), + Effect.tapError(() => + fileSystem.remove(partPath, { force: true }).pipe(Effect.ignore), + ), + ), ), Effect.as(true), Effect.catch((cause) => From 7c61f6861e3a4db9479286e3777435dc14c918ff Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 07:53:42 +0000 Subject: [PATCH 4/5] fix(server): tool images never serve an attachment id the provider supplied An oversized image block that carried both base64 data and its own attachmentId kept that id: the guard only looked at blocks without data, and stripping the oversized bytes then left a reference AssetAccess would serve. Any attachmentId already present on a served image block is now dropped before any other step, so only ids the server assigns survive. AI-assisted; verified with the tests and tools listed above. --- .../toolOutputImageBytes.test.ts | 21 +++++++++++++++++++ .../orchestration-v2/toolOutputImageBytes.ts | 13 +++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts index dc97908aeacd0..120283bae392b 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts @@ -225,6 +225,27 @@ describe("stripUnservedToolOutputImageBytes", () => { }); describe("storeServedToolOutputImages", () => { + effectIt.effect("drops attachment ids the provider put in its own image blocks", () => + Effect.gen(function* () { + const attachmentId = "other-thread-00000000-0000-4000-8000-000000000001-png"; + for (const data of [undefined, "A".repeat(MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 4)]) { + const item = toolItem({ + content: [{ type: "image", mimeType: "image/png", data, attachmentId }], + }); + const stored = stripUnservedToolOutputImageBytes(yield* storeServedToolOutputImages(item)); + expect( + toolOutputImages(stored.type === "dynamic_tool" ? stored.output : null)[0]?.attachmentId, + ).toBeUndefined(); + } + }).pipe( + Effect.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "tool-output-images-" }).pipe( + Layer.provideMerge(NodeServices.layer), + ), + ), + ), + ); + effectIt.effect("keeps an image inline until a retried write stores all of its bytes", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts index 4f2a3db8d4e81..d38b2b1c10a0a 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts @@ -150,11 +150,13 @@ export const storeServedToolOutputImages = Effect.fn( blocks, Effect.fnUntraced(function* (block, index) { const image = readToolOutputImage(block); - if (image === null) return; - const reference = { type: "image", mimeType: image.mimeType }; - // Provider output must not point at another thread's stored file. + if (image === null || !Predicate.isObject(block)) return; + // Provider output must not point at another thread's stored file, on any path below. + if (Object.hasOwn(block, "attachmentId")) { + const { attachmentId: _providerId, ...rest } = block; + replacements.set(block, rest); + } if (image.data === undefined || image.data.length > MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH) { - if (image.attachmentId !== undefined) replacements.set(block, reference); return; } const decoded = Base64.decode(image.data); @@ -191,7 +193,8 @@ export const storeServedToolOutputImages = Effect.fn( }).pipe(Effect.as(false)), ), ); - if (stored) replacements.set(block, { ...reference, attachmentId }); + if (stored) + replacements.set(block, { type: "image", mimeType: image.mimeType, attachmentId }); }), { concurrency: 1, discard: true }, ); From 87e93183f9a1185850970f8670c529805f78c914 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 16:35:41 +0000 Subject: [PATCH 5/5] fix(server): stored tool images resolve only to the bytes their id addresses Rows persisted before this change can hold any attachmentId a provider emitted, and the ingest sanitizer only covers new updates. Tool-output image ids are now a content address (sha256 of the bytes), and the asset reader serves a referenced file only when its bytes hash back to that id, so a provider-supplied id in a historical or replayed row cannot reach another thread's attachment. Identical images now share one file. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/assets/AssetAccess.test.ts | 31 +++++++++++++------ apps/server/src/assets/AssetAccess.ts | 13 ++++++-- apps/server/src/attachmentStore.ts | 7 +++++ .../orchestration-v2/toolOutputImageBytes.ts | 13 +++----- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts index 703724174279f..a0a90df521a25 100644 --- a/apps/server/src/assets/AssetAccess.test.ts +++ b/apps/server/src/assets/AssetAccess.test.ts @@ -27,6 +27,7 @@ import { vi } from "vite-plus/test"; import * as AssetSigner from "./AssetSigner.ts"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; +import { toolOutputImageAttachmentId } from "../attachmentStore.ts"; import * as ServerConfig from "../config.ts"; import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts"; @@ -102,7 +103,8 @@ const oversizedScreenshotItem = { }, }; -const storedScreenshotAttachmentId = "thread-1-00000000-0000-4000-8000-000000000041-png"; +const storedScreenshotAttachmentId = toolOutputImageAttachmentId(screenshotPng, ".png"); +const foreignAttachmentId = "thread-2-00000000-0000-4000-8000-000000000041-png"; const storedScreenshotItem = { ...screenshotItem, id: TurnItemId.make("tool-screenshot-stored"), @@ -110,13 +112,21 @@ const storedScreenshotItem = { content: [{ type: "image", mimeType: "image/png", attachmentId: storedScreenshotAttachmentId }], }, }; +// Rows persisted before the server stored images can carry any id a provider emitted. +const foreignScreenshotItem = { + ...screenshotItem, + id: TurnItemId.make("tool-screenshot-foreign"), + output: { + content: [{ type: "image", mimeType: "image/png", attachmentId: foreignAttachmentId }], + }, +}; const layerTest = Layer.mergeAll( NodeHttpPlatform.layer, Layer.mock(Orchestrator.OrchestratorV2)({ getTurnItem: ({ itemId }) => Effect.succeed( - [screenshotItem, oversizedScreenshotItem, storedScreenshotItem].find( + [screenshotItem, oversizedScreenshotItem, storedScreenshotItem, foreignScreenshotItem].find( (item) => item.id === itemId, ) ?? null, ), @@ -199,10 +209,9 @@ describe("AssetAccess", () => { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; yield* fs.makeDirectory(config.attachmentsDir, { recursive: true }); - yield* fs.writeFile( - path.join(config.attachmentsDir, `${storedScreenshotAttachmentId}.png`), - screenshotPng, - ); + for (const attachmentId of [storedScreenshotAttachmentId, foreignAttachmentId]) { + yield* fs.writeFile(path.join(config.attachmentsDir, `${attachmentId}.png`), screenshotPng); + } const resource = { _tag: "tool-output-image" as const, threadId: screenshotItem.threadId, @@ -226,10 +235,12 @@ describe("AssetAccess", () => { Effect.flip, ); expect(missing._tag).toBe("AssetWorkspaceAssetNotFoundError"); - const oversized = yield* issueAssetUrl({ - resource: { ...resource, itemId: oversizedScreenshotItem.id }, - }).pipe(Effect.flip); - expect(oversized._tag).toBe("AssetWorkspaceAssetNotFoundError"); + for (const itemId of [oversizedScreenshotItem.id, foreignScreenshotItem.id]) { + const unserved = yield* issueAssetUrl({ resource: { ...resource, itemId } }).pipe( + Effect.flip, + ); + expect(unserved._tag).toBe("AssetWorkspaceAssetNotFoundError"); + } }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts index cffedb8760697..2c5bb80d757c9 100644 --- a/apps/server/src/assets/AssetAccess.ts +++ b/apps/server/src/assets/AssetAccess.ts @@ -50,7 +50,12 @@ import * as Stream from "effect/Stream"; import { base64UrlDecodeUtf8, base64UrlEncode } from "../auth/utils.ts"; import * as AssetSigner from "./AssetSigner.ts"; import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts"; -import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts"; +import { + parseAttachmentFileExtension, + resolveAttachmentPathById, + toolOutputImageAttachmentId, +} from "../attachmentStore.ts"; +import { inferImageExtension } from "../imageMime.ts"; import * as ServerConfig from "../config.ts"; import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; @@ -251,7 +256,11 @@ const readToolOutputImage = Effect.fn("AssetAccess.readToolOutputImage")(functio }); if (filePath === null) return null; const fileSystem = yield* FileSystem.FileSystem; - return { mimeType: image.mimeType, bytes: yield* fileSystem.readFile(filePath) }; + const bytes = yield* fileSystem.readFile(filePath); + const extension = inferImageExtension({ mimeType: image.mimeType }); + return toolOutputImageAttachmentId(bytes, extension) === image.attachmentId + ? { mimeType: image.mimeType, bytes } + : null; } return image?.data === undefined || image.data.length > MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH ? null diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.ts index a2b531e3fa5b0..3aa3b9baa7b62 100644 --- a/apps/server/src/attachmentStore.ts +++ b/apps/server/src/attachmentStore.ts @@ -116,6 +116,13 @@ export function chatAttachmentStableKey( return `${messageId}:${index}:${digest}`; } +/** Content-addressed: the asset reader re-derives it from the file's bytes, so an id that + * provider output carried in a stored row can never resolve to a file it did not hash to. */ +export function toolOutputImageAttachmentId(bytes: Uint8Array, extension: string): string { + const digest = NodeCrypto.createHash("sha256").update(bytes).digest("hex"); + return `${createDeterministicAttachmentId("tool_output", digest)}${attachmentIdExtensionSuffix(extension)}`; +} + export function parseThreadSegmentFromAttachmentId(attachmentId: string): string | null { const normalizedId = normalizeAttachmentRelativePath(attachmentId); if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) { diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts index d38b2b1c10a0a..f17dc75a58dac 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts @@ -14,7 +14,7 @@ import { toolOutputImageBlocks, } from "@t3tools/shared/toolOutput"; -import { chatAttachmentStableKey, createDeterministicAttachmentId } from "../attachmentStore.ts"; +import { toolOutputImageAttachmentId } from "../attachmentStore.ts"; import * as ServerConfig from "../config.ts"; import { inferImageExtension } from "../imageMime.ts"; @@ -135,7 +135,7 @@ function mapOutputBlocks(value: unknown, map: (block: unknown) => unknown): unkn } /** Moves served image bytes to the attachment store so SQLite rows keep only a reference. - * Ids are deterministic per item, index, and bytes, so repeated updates reuse one file. */ + * Ids address the bytes, so repeated updates and identical images share one file. */ export const storeServedToolOutputImages = Effect.fn( "ToolOutputImageBytes.storeServedToolOutputImages", )(function* (item: OrchestrationV2TurnItem) { @@ -148,7 +148,7 @@ export const storeServedToolOutputImages = Effect.fn( const replacements = new Map(); yield* Effect.forEach( blocks, - Effect.fnUntraced(function* (block, index) { + Effect.fnUntraced(function* (block) { const image = readToolOutputImage(block); if (image === null || !Predicate.isObject(block)) return; // Provider output must not point at another thread's stored file, on any path below. @@ -162,13 +162,8 @@ export const storeServedToolOutputImages = Effect.fn( const decoded = Base64.decode(image.data); if (Result.isFailure(decoded)) return; const bytes = decoded.success; - const baseId = createDeterministicAttachmentId( - item.threadId, - chatAttachmentStableKey(item.id, index, bytes), - ); - if (baseId === null) return; const extension = inferImageExtension({ mimeType: image.mimeType }); - const attachmentId = `${baseId}-${extension.slice(1)}`; + const attachmentId = toolOutputImageAttachmentId(bytes, extension); const filePath = path.join(config.attachmentsDir, `${attachmentId}${extension}`); const partPath = `${filePath}.${NodeCrypto.randomUUID()}.part`; const stored = yield* fileSystem.exists(filePath).pipe(