Skip to content

The agent that grows with you

fix(desktop): avoid Explorer DnD image preview freeze on Windows

stantheman01289d7e50c4964d
diff --git a/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts b/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts
index 32e4a93c8a7..0a44c6a4452 100644
--- a/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts
+++ b/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts
@@ -12,6 +12,7 @@ import {
extractDroppedFiles,
HERMES_PATHS_MIME,
partitionDroppedFiles,
+ resolveImageAttachmentPreview,
useComposerActions
} from './use-composer-actions'
@@ -393,12 +394,10 @@ describe('useComposerActions native image drops', () => {
expect(attached).toBe(true)
expect(saveImageBuffer).toHaveBeenCalledWith(expect.any(Uint8Array), '.png', 'Screen Shot 2026-08-11.png')
- expect(readFileDataUrl).toHaveBeenCalledWith(durablePath)
- expect(readFileDataUrl).not.toHaveBeenCalledWith(transientPath)
- // The bounded-preview pipeline no longer retains the full-resolution data
- // URL on the attachment (`previewUrl`); the durable path is the authority
- // and the display thumbnail resolves asynchronously. What matters here is
- // that the attachment is keyed to the DURABLE path, not the transient one.
+ // The in-hand blob backs the chip preview (object URL — #63682), so the
+ // durable path is never base64-read over IPC either.
+ expect(readFileDataUrl).not.toHaveBeenCalled()
+ // The attachment is keyed to the DURABLE path, not the transient one.
expect(add).toHaveBeenCalledWith(
expect.objectContaining({
kind: 'image',
@@ -793,3 +792,49 @@ describe('attachImagePath thumbnail separation', () => {
expect(attachment?.thumbnailUrl).toBeUndefined()
})
})
+
+describe('resolveImageAttachmentPreview', () => {
+ const LOCAL_PREVIEW = 'data:image/png;base64,bG9jYWw='
+
+ afterEach(() => {
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+ $connection.set(null)
+ })
+
+ it('uses an object URL for an in-hand File/Blob (OS Explorer drop) and skips IPC base64', async () => {
+ const readFileDataUrl = vi.fn(async () => LOCAL_PREVIEW)
+ const createObjectURL = vi.fn(() => 'blob:hermes-preview-1')
+
+ vi.stubGlobal('window', { hermesDesktop: { readFileDataUrl } })
+ vi.stubGlobal('URL', { ...URL, createObjectURL, revokeObjectURL: vi.fn() })
+
+ const file = new File([new Uint8Array([1, 2, 3, 4])], 'Lattice.png', { type: 'image/png' })
+ const preview = await resolveImageAttachmentPreview('C:\\Users\\Administrator\\Desktop\\Lattice.png', file)
+
+ expect(preview).toBe('blob:hermes-preview-1')
+ expect(createObjectURL).toHaveBeenCalledWith(file)
+ // The freeze path: never base64-load the dropped image over IPC.
+ expect(readFileDataUrl).not.toHaveBeenCalled()
+ })
+
+ it('falls back to IPC data-URL preview when only a path is available (paperclip)', async () => {
+ const readFileDataUrl = vi.fn(async () => LOCAL_PREVIEW)
+
+ vi.stubGlobal('window', { hermesDesktop: { readFileDataUrl } })
+
+ await expect(resolveImageAttachmentPreview('/Users/me/Pictures/pic.png')).resolves.toBe(LOCAL_PREVIEW)
+ expect(readFileDataUrl).toHaveBeenCalledWith('/Users/me/Pictures/pic.png')
+ })
+
+ it('ignores an empty Blob and falls back to the path preview', async () => {
+ const readFileDataUrl = vi.fn(async () => LOCAL_PREVIEW)
+
+ vi.stubGlobal('window', { hermesDesktop: { readFileDataUrl } })
+
+ const empty = new Blob([])
+
+ await expect(resolveImageAttachmentPreview('/tmp/shot.png', empty)).resolves.toBe(LOCAL_PREVIEW)
+ expect(readFileDataUrl).toHaveBeenCalledWith('/tmp/shot.png')
+ })
+})
diff --git a/apps/desktop/src/app/chat/hooks/use-composer-actions.ts b/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
index e57ccb72b3a..df261df5acb 100644
--- a/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
+++ b/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
@@ -77,6 +77,24 @@ async function queuedAttachmentPreview(filePath: string): Promise<{ previewUrl:
return task
}
+/**
+ * Prefer a cheap object-URL preview when the drop/paste still has a Blob/File
+ * handle. `readFileDataUrl` base64-loads the whole file over IPC (capped at
+ * 16 MB) and was freezing Desktop on Windows Explorer image drops (#63682).
+ * Object URLs skip that read; path-only attaches (paperclip) still fall back
+ * to the IPC data-URL path.
+ */
+export async function resolveImageAttachmentPreview(
+ filePath: string,
+ previewSource?: Blob | null
+): Promise<string> {
+ if (previewSource && previewSource.size > 0) {
+ return URL.createObjectURL(previewSource)
+ }
+
+ return attachmentPreviewDataUrl(filePath)
+}
+
export interface DroppedFile {
/** Browser-native File handle. Absent for in-app drags (e.g. project tree). */
file?: File
@@ -465,7 +483,7 @@ export function useComposerActions({
)
const attachImagePath = useCallback(
- async (filePath: string) => {
+ async (filePath: string, previewSource?: Blob | null) => {
if (!filePath) {
return false
}
@@ -482,6 +500,18 @@ export function useComposerActions({
attachToMain(baseAttachment)
try {
+ // OS drops / clipboard blobs pass their File/Blob so preview never
+ // base64-loads the full image over IPC (Windows freeze on Explorer
+ // drag-drop — #63682). Path-only picks keep the queued IPC thumbnail
+ // path; blob previews skip the read entirely.
+ if (previewSource && previewSource.size > 0) {
+ const previewUrl = URL.createObjectURL(previewSource)
+
+ scope.updateIfCurrent(baseAttachment, { previewUrl })
+
+ return true
+ }
+
const { previewUrl, thumbnailUrl } = await queuedAttachmentPreview(filePath)
if (previewUrl) {
@@ -532,7 +562,10 @@ export function useComposerActions({
return false
}
- return isCurrent() ? attachImagePath(savedPath) : false
+ // Reuse the in-hand blob for the chip preview — do not re-read the
+ // just-written temp file as a data URL. A late component unmount must
+ // not leak the attach: attach only while still current.
+ return isCurrent() ? attachImagePath(savedPath, blob) : false
} catch (err) {
notifyError(err, copy.imageAttachFailed)
@@ -715,14 +748,15 @@ export function useComposerActions({
const isImage = file.type.startsWith('image/') || isImagePath(file.name) || (filePath && isImagePath(filePath))
if (isImage) {
- // Finder may expose a dropped screenshot through a short-lived
- // TemporaryItems/NSIRD_screencaptureui path even when the visible
- // file has already landed on Desktop. Reading that path for the
- // preview can succeed, then image.attach fails after macOS removes
- // it before submit. Persist the File bytes into Desktop's durable
- // composer-image cache first; keep the native path as a compatibility
- // fallback for older shells that cannot save the buffer.
- if ((await attachImageBlob(file)) || (filePath && (await attachImagePath(filePath)))) {
+ // Persist the File bytes into Desktop's durable composer-image cache
+ // FIRST: Finder may expose a dropped screenshot through a short-lived
+ // TemporaryItems/NSIRD_screencaptureui path even when the visible file
+ // has already landed on Desktop — reading that path for the preview
+ // can succeed, then image.attach fails after macOS removes it before
+ // submit. attachImageBlob also hands the in-hand blob through for a
+ // non-blocking object-URL chip preview (#63682); the native path stays
+ // the fallback for shells that cannot save the buffer.
+ if ((await attachImageBlob(file)) || (filePath && (await attachImagePath(filePath, file)))) {
attached = true
continue
diff --git a/apps/desktop/src/lib/chat-runtime.test.ts b/apps/desktop/src/lib/chat-runtime.test.ts
index b8c91b02e49..b407780a552 100644
--- a/apps/desktop/src/lib/chat-runtime.test.ts
+++ b/apps/desktop/src/lib/chat-runtime.test.ts
@@ -74,6 +74,15 @@ describe('optimisticAttachmentRef', () => {
expect(optimisticAttachmentRef(attachment({ kind: 'image', previewUrl: 'https://example.com/x.png' }))).toBeNull()
})
+ it('renders an OS-drop blob: preview as a markdown image (no IPC data URL)', () => {
+ const blobUrl = 'blob:https://desktop/preview-1'
+ const ref = optimisticAttachmentRef(
+ attachment({ kind: 'image', label: 'Lattice.png', detail: 'C:\\shot.png', previewUrl: blobUrl })
+ )
+
+ expect(ref).toBe(`![Lattice.png](${blobUrl})`)
+ })
+
it('passes non-image attachments straight through to attachmentDisplayText', () => {
expect(optimisticAttachmentRef(attachment({ kind: 'file', refText: '@file:src/a.ts', previewUrl: DATA_URL }))).toBe(
'@file:src/a.ts'
diff --git a/apps/desktop/src/lib/chat-runtime.ts b/apps/desktop/src/lib/chat-runtime.ts
index 363b188362d..a88b11d4b59 100644
--- a/apps/desktop/src/lib/chat-runtime.ts
+++ b/apps/desktop/src/lib/chat-runtime.ts
@@ -221,7 +221,8 @@ export function attachmentDisplayText(attachment: ComposerAttachment): string |
* URL renders inline with zero network, while an `@image:<localpath>` ref would
* route through `/api/media` and can 403 in remote mode. Full-resolution bytes
* are loaded separately for the model and on-demand lightbox, not retained in
- * the optimistic message.
+ * the optimistic message. `blob:` previews from OS drops bypass the data-URL
+ * extract path and render as a markdown image instead (#63682).
*
* Everything else (files, folders, terminals, post-sync `@file:` refs) falls
* through to `attachmentDisplayText`.
@@ -232,6 +233,15 @@ export function optimisticAttachmentRef(attachment: ComposerAttachment): string
}
if (attachment.kind === 'image') {
+ // Object-URL previews from OS drops take precedence over the path ref:
+ // markdown image keeps them out of the data-URL extract path while still
+ // rendering inline in the optimistic bubble (#63682).
+ if (attachment.previewUrl?.startsWith('blob:')) {
+ const alt = attachment.label || 'image'
+
+ return `![${alt}](${attachment.previewUrl})`
+ }
+
// Prefer a filesystem-backed `@image:<path>` ref so the in-flight bubble
// renders through the same DirectiveImage path as a reloaded turn. That
// component shows a bounded thumbnail inline (no full-resolution paint, so
diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.ts
index 2407a12e27c..be82b9b8d27 100644
--- a/apps/desktop/src/store/composer.ts
+++ b/apps/desktop/src/store/composer.ts
@@ -3,6 +3,17 @@ import { atom } from 'nanostores'
import { deriveDraftTitle } from '@/lib/draft-title'
import { triggerHaptic } from '@/lib/haptics'
+/** Release blob: chip previews created for OS image drops (see #63682). */
+function revokeAttachmentPreviewUrl(url?: string | null) {
+ if (url?.startsWith('blob:')) {
+ try {
+ URL.revokeObjectURL(url)
+ } catch {
+ // Best-effort — a revoked/invalid URL must not break chip removal.
+ }
+ }
+}
+
export interface ComposerAttachment {
id: string
/** Renderer-lifetime identity for one attachment occurrence. Unlike `id`,
@@ -93,12 +104,17 @@ export function createComposerAttachmentScope($attachments = atom<ComposerAttach
}
},
clear() {
+ for (const attachment of $attachments.get()) {
+ revokeAttachmentPreviewUrl(attachment.previewUrl)
+ }
+
$attachments.set([])
},
remove(id) {
const current = $attachments.get()
const removed = current.find(attachment => attachment.id === id) || null
$attachments.set(current.filter(attachment => attachment.id !== id))
+ revokeAttachmentPreviewUrl(removed?.previewUrl)
return removed
},
@@ -144,10 +160,15 @@ export function createComposerAttachmentScope($attachments = atom<ComposerAttach
return false
}
+ const previous = current[index]
const next = [...current]
next[index] = attachment
$attachments.set(next)
+ if (previous?.previewUrl && previous.previewUrl !== attachment.previewUrl) {
+ revokeAttachmentPreviewUrl(previous.previewUrl)
+ }
+
return true
},
updateIfCurrent(expected, patch) {
@@ -721,8 +742,13 @@ function upsertAttachment(attachments: ComposerAttachment[], attachment: Compose
return [...attachments, attachment]
}
+ const previous = attachments[index]
const next = [...attachments]
next[index] = attachment
+ if (previous?.previewUrl && previous.previewUrl !== attachment.previewUrl) {
+ revokeAttachmentPreviewUrl(previous.previewUrl)
+ }
+
return next
}