Skip to content

The agent that grows with you

fix(desktop): stop trusting composer previewUrl as a full-res upload cache

chelsealong8b228c37d588
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 6fda05e4f61..a3d8c8346ec 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -5645,14 +5645,16 @@ describe('uploadComposerAttachment remote read failures', () => {
})
})
-describe('uploadComposerAttachment preview reuse', () => {
+describe('uploadComposerAttachment image cache contract', () => {
afterEach(() => {
vi.restoreAllMocks()
})
- it('reuses the chip previewUrl instead of re-reading the image off disk', async () => {
- // attachImagePath already read the full file for the thumbnail; submit
- // must not pay the disk read + IPC round-trip a second time.
+ it('always re-reads the image from disk, even when a stale previewUrl is cached', async () => {
+ // attachImagePath drops `previewUrl` as soon as a thumbnail exists, so a
+ // populated `previewUrl` here does not mean it holds full-resolution
+ // bytes (#93324) — the upload must never trust it and must always read
+ // the on-disk file for the bytes the model receives.
const readFileDataUrl = vi.fn(async () => 'data:image/png;base64,ZnJvbS1kaXNr')
Object.defineProperty(window, 'hermesDesktop', {
configurable: true,
@@ -5673,44 +5675,7 @@ describe('uploadComposerAttachment preview reuse', () => {
kind: 'image',
label: 'shot.png',
path: '/local/shot.png',
- previewUrl: 'data:image/png;base64,ZnJvbS1wcmV2aWV3'
- },
- { remote: true, requestGateway, sessionId: RUNTIME_SESSION_ID }
- )
-
- expect(readFileDataUrl).not.toHaveBeenCalled()
- expect(requestGateway).toHaveBeenCalledWith('image.attach_bytes', {
- content_base64: 'ZnJvbS1wcmV2aWV3',
- filename: 'shot.png',
- session_id: RUNTIME_SESSION_ID
- })
- expect(uploaded.path).toBe('/gw/images/shot.png')
- })
-
- it('falls back to the disk read when previewUrl is not a base64 data URL', async () => {
- // A non-data previewUrl (e.g. a gateway media URL) carries no bytes —
- // the upload must still read the real file.
- const readFileDataUrl = vi.fn(async () => 'data:image/png;base64,ZnJvbS1kaXNr')
- Object.defineProperty(window, 'hermesDesktop', {
- configurable: true,
- value: { readFileDataUrl }
- })
-
- const requestGateway = vi.fn(async (method: string) => {
- if (method === 'image.attach_bytes') {
- return { attached: true, path: '/gw/images/shot.png' } as never
- }
-
- return {} as never
- })
-
- await uploadComposerAttachment(
- {
- id: 'image:shot.png',
- kind: 'image',
- label: 'shot.png',
- path: '/local/shot.png',
- previewUrl: 'https://gateway.example/media/shot.png'
+ previewUrl: 'data:image/png;base64,c3RhbGUtY2FjaGVk'
},
{ remote: true, requestGateway, sessionId: RUNTIME_SESSION_ID }
)
@@ -5721,6 +5686,7 @@ describe('uploadComposerAttachment preview reuse', () => {
filename: 'shot.png',
session_id: RUNTIME_SESSION_ID
})
+ expect(uploaded.path).toBe('/gw/images/shot.png')
})
})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index e0dc5e30968..c10857050f6 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -122,16 +122,17 @@ export async function uploadComposerAttachment(
// Read bytes/paths ONCE, outside the retry. Only the session-scoped RPC is
// replayed on recovery — re-reading a multi-MB file to retry a dead session
- // id would double the disk/IPC cost of every recovered attach. For images,
- // the chip's previewUrl already holds the full file as a base64 data URL,
- // so passing it avoids re-reading the same bytes off disk at submit.
+ // id would double the disk/IPC cost of every recovered attach. Images are
+ // always read fresh from disk here: the chip's cached `previewUrl` is
+ // dropped once a thumbnail exists, so it cannot be trusted to hold the
+ // full-resolution bytes the model needs.
let imagePayload: Awaited<ReturnType<typeof readImageForRemoteAttach>> | null = null
let fileDataUrl: null | string = null
if (uploadBytes) {
try {
if (attachment.kind === 'image') {
- imagePayload = await readImageForRemoteAttach(path, attachment.previewUrl)
+ imagePayload = await readImageForRemoteAttach(path)
} else {
fileDataUrl = await readFileDataUrlForAttach(path)
}
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
index 59485978afa..4c8e20a00f5 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
@@ -421,24 +421,14 @@ export function imageFilenameFromPath(filePath: string): string {
// not the gateway's, so read the bytes here and upload them via
// image.attach_bytes. Returns null when the file can't be read.
//
-// `cachedDataUrl` is the attachment's `previewUrl` when the composer already
-// read the file for the chip thumbnail — that preview is the FULL file as a
-// base64 data URL (attachmentPreviewDataUrl → readFileDataUrl), not a
-// downscaled copy, so reusing it skips a second disk read + IPC round-trip of
-// the same bytes at submit. Only a `;base64,` data URL qualifies; anything
-// else falls through to the disk read.
+// Always re-reads from disk rather than trusting `attachment.previewUrl` as a
+// cache: once a thumbnail is generated, `attachImagePath` keeps only the
+// bounded (≤512px) `thumbnailUrl` and drops `previewUrl` — so a cached
+// `previewUrl` is never guaranteed to be the full-resolution bytes the model
+// needs, and trusting it here would risk silently uploading a downscaled copy.
export async function readImageForRemoteAttach(
- filePath: string,
- cachedDataUrl?: string
+ filePath: string
): Promise<{ contentBase64: string; filename: string } | null> {
- if (cachedDataUrl?.includes(';base64,')) {
- const cached = base64FromDataUrl(cachedDataUrl)
-
- if (cached) {
- return { contentBase64: cached, filename: imageFilenameFromPath(filePath) }
- }
- }
-
const dataUrl = await window.hermesDesktop?.readFileDataUrl(filePath)
const contentBase64 = dataUrl ? base64FromDataUrl(dataUrl) : ''