Date: Sun, 4 Oct 2026 22:54:41 -0700
Subject: [PATCH 20/24] fix(feedback): preserve mixed paste text and seed read
queue on attach
---
.../sidebar/SidebarFeedbackDialog.test.tsx | 102 +++++++++++++++++-
.../sidebar/SidebarFeedbackDialog.tsx | 7 +-
.../sidebar/use-sidebar-feedback-images.ts | 5 +-
.../src/lib/feedback-image-attachments.ts | 11 +-
4 files changed, 115 insertions(+), 10 deletions(-)
diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx
index 0039acdb1955..8f7d75733940 100644
--- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx
+++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx
@@ -2,6 +2,8 @@
import React, { act, type ReactNode } from 'react'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import type * as FeedbackImageAttachments from '@/lib/feedback-image-attachments'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
@@ -195,6 +197,100 @@ describe('SidebarFeedbackDialog environment prefill', () => {
})
describe('SidebarFeedbackDialog image submission', () => {
+ it.each(['refused', 'shrunk'] as const)(
+ 'preserves text and its caret when an oversized image is %s after paste',
+ async (result) => {
+ const user = userEvent.setup()
+ const bytes = new Uint8Array(53)
+ bytes.set([137, 80, 78, 71, 13, 10, 26, 10])
+ const header = new DataView(bytes.buffer)
+ header.setUint32(8, 13)
+ bytes.set(new TextEncoder().encode('IHDR'), 12)
+ header.setUint32(16, 1)
+ header.setUint32(20, 1)
+ header.setUint32(33, 8)
+ bytes.set(new TextEncoder().encode(result === 'refused' ? 'acTL' : 'IDAT'), 37)
+ const file = new File([bytes], 'capture.png', { type: 'image/png' })
+ Object.defineProperty(file, 'size', { value: 6_000_000 })
+ let finishRead:
+ | ((
+ value: Awaited
>
+ ) => void)
+ | undefined
+ mocks.readFeedbackImageFiles.mockReturnValue(
+ new Promise((resolve) => {
+ finishRead = resolve
+ })
+ )
+ render()
+ const textarea = screen.getByPlaceholderText('What could we improve?')
+ await waitFor(() => expect(textarea.value).toContain('Orca:'))
+ fireEvent.change(textarea, { target: { value: 'before after' } })
+ textarea.focus()
+ textarea.setSelectionRange(7, 7)
+ const clipboard = new DataTransfer()
+ clipboard.setData('text/plain', 'report')
+ Object.defineProperty(clipboard, 'files', { value: [file] })
+
+ await user.paste(clipboard)
+ expect(textarea.value).toBe('before reportafter')
+ expect(textarea.selectionStart).toBe(13)
+ await user.keyboard('!')
+ expect(textarea.value).toBe('before report!after')
+ await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalled())
+ const actual = await vi.importActual(
+ '@/lib/feedback-image-attachments'
+ )
+ const readResult =
+ result === 'refused'
+ ? await actual.readFeedbackImageFiles([file], 0)
+ : {
+ images: [
+ {
+ id: 'shrunk',
+ name: 'capture.png',
+ contentType: 'image/png',
+ bytes: 100,
+ data: new Uint8Array([1]),
+ previewUrl: 'blob:shrunk'
+ }
+ ],
+ errors: [],
+ notices: []
+ }
+ await act(async () => {
+ finishRead?.(readResult)
+ })
+ expect(textarea.value).toBe('before report!after')
+ expect(textarea.selectionStart).toBe(14)
+ if (result === 'refused') {
+ expect(mocks.toastWarning).toHaveBeenCalledWith('capture.png is larger than 4.0 MB.')
+ expect(screen.queryByRole('button', { name: 'Remove capture.png' })).toBeNull()
+ } else {
+ expect(screen.getByRole('button', { name: 'Remove capture.png' })).not.toBeNull()
+ }
+ }
+ )
+
+ it('still consumes mixed text when the image fits without shrinking', async () => {
+ const user = userEvent.setup()
+ mocks.readFeedbackImageFiles.mockResolvedValue({ images: [], errors: [], notices: [] })
+ render()
+ const textarea = screen.getByPlaceholderText('What could we improve?')
+ await waitFor(() => expect(textarea.value).toContain('Orca:'))
+ fireEvent.change(textarea, { target: { value: 'report' } })
+ textarea.focus()
+ textarea.setSelectionRange(6, 6)
+ const clipboard = new DataTransfer()
+ clipboard.setData('text/plain', 'image metadata')
+ const file = new File(['image'], 'small.png', { type: 'image/png' })
+ Object.defineProperty(clipboard, 'files', { value: [file] })
+ await user.paste(clipboard)
+ expect(textarea.value).toBe('report')
+ expect(textarea.selectionStart).toBe(6)
+ await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledWith([file], 0, 0))
+ })
+
it('keeps the dialog scrollable within short windows', () => {
const { container } = render()
const content = container.querySelector('.scrollbar-sleek')
@@ -445,7 +541,7 @@ describe('SidebarFeedbackDialog image submission', () => {
Object.defineProperty(file, 'size', { value: 4 * 1024 * 1024 + 1 })
const paste = new Event('paste', { bubbles: true, cancelable: true })
Object.defineProperty(paste, 'clipboardData', {
- value: { files: [file] }
+ value: { files: [file], getData: () => '' }
})
fireEvent(textarea, paste)
@@ -482,7 +578,7 @@ describe('SidebarFeedbackDialog image submission', () => {
const small = new File(['x'], 'small.png', { type: 'image/png' })
Object.defineProperty(small, 'size', { value: 1024 })
const paste = new Event('paste', { bubbles: true, cancelable: true })
- Object.defineProperty(paste, 'clipboardData', { value: { files: [small] } })
+ Object.defineProperty(paste, 'clipboardData', { value: { files: [small], getData: () => '' } })
fireEvent(screen.getByPlaceholderText('What could we improve?'), paste)
expect(paste.defaultPrevented).toBe(false)
@@ -519,7 +615,7 @@ describe('SidebarFeedbackDialog image submission', () => {
const file = new File(['x'], `${name}.png`, { type: 'image/png' })
Object.defineProperty(file, 'size', { value: 6 * 1024 * 1024 })
const paste = new Event('paste', { bubbles: true, cancelable: true })
- Object.defineProperty(paste, 'clipboardData', { value: { files: [file] } })
+ Object.defineProperty(paste, 'clipboardData', { value: { files: [file], getData: () => '' } })
fireEvent(textarea, paste)
return paste
}
diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx
index deb113a1ebb8..7be16e4d9eaf 100644
--- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx
+++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx
@@ -263,7 +263,12 @@ export function SidebarFeedbackDialog({
// An unsupported image still routes through for its rejection toast,
// but preventing default there would silently eat co-pasted text.
const reserved = getReservedImageCapacity()
- if (hasAttachableFeedbackImage(pasted, reserved.count, reserved.bytes)) {
+ // An optimistic shrink may fail; let co-pasted text insert now rather than restore it later.
+ if (
+ hasAttachableFeedbackImage(pasted, reserved.count, reserved.bytes, {
+ allowShrinking: event.clipboardData.getData('text/plain').length === 0
+ })
+ ) {
event.preventDefault()
}
handleAddFiles(pasted)
diff --git a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts
index 12febc03d22a..385e80ddf20f 100644
--- a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts
+++ b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts
@@ -38,8 +38,7 @@ export function useSidebarFeedbackImages(params: {
const pendingImageReadsRef = useRef({ count: 0, bytes: 0 })
// Why: a shrunk image's size is unknown until it is read, so batches read one
// at a time, each sized against what the batches before it actually committed.
- const readQueueRef = useRef>(undefined!)
- readQueueRef.current ??= Promise.resolve()
+ const readQueueRef = useRef | null>(null)
const clearImages = useCallback(() => {
liveImageDraftsRef.current.forEach(releaseFeedbackImageDraft)
@@ -89,7 +88,7 @@ export function useSidebarFeedbackImages(params: {
pendingReads.count += files.length
pendingReads.bytes += batchBytes
setPendingImageReadCount((current) => current + files.length)
- const read = readQueueRef.current.then(() => {
+ const read = (readQueueRef.current ?? Promise.resolve()).then(() => {
// Why: an unmounted dialog discards whatever this reads, so skip the decode and re-encodes.
if (!params.mountedRef.current) {
return { images: [], errors: [], notices: [] }
diff --git a/src/renderer/src/lib/feedback-image-attachments.ts b/src/renderer/src/lib/feedback-image-attachments.ts
index f1e60404a6c8..13f798a926a1 100644
--- a/src/renderer/src/lib/feedback-image-attachments.ts
+++ b/src/renderer/src/lib/feedback-image-attachments.ts
@@ -94,18 +94,23 @@ function canAttachWithin(file: File, fitBytes: number): boolean {
* nothing is attachable would also discard any text riding along on the
* clipboard. Every limit readFeedbackImageFiles checks from a file's type and
* size is mirrored here. Checks that need its bytes (dimensions, APNG, whether a
- * shrink fits) cannot run synchronously, so a paste they refuse loses its text.
+ * shrink fits) cannot run synchronously; mixed text pastes require an as-is fit.
*/
export function hasAttachableFeedbackImage(
files: readonly File[],
existingCount = 0,
- existingBytes = 0
+ existingBytes = 0,
+ options: { allowShrinking?: boolean } = {}
): boolean {
const fitBytes = feedbackImageFitBytes(MAX_FEEDBACK_IMAGE_TOTAL_BYTES - existingBytes)
return (
existingCount < MAX_FEEDBACK_IMAGE_COUNT &&
files.some(
- (file) => isSupportedType(file.type) && file.size > 0 && canAttachWithin(file, fitBytes)
+ (file) =>
+ isSupportedType(file.type) &&
+ file.size > 0 &&
+ (file.size <= fitBytes ||
+ (options.allowShrinking !== false && canAttachWithin(file, fitBytes)))
)
)
}
From 91e754875a4e06e5fdea283af8ba5d25ca923356 Mon Sep 17 00:00:00 2001
From: Neil <4138956+nwparker@users.noreply.github.com>
Date: Mon, 5 Oct 2026 16:42:16 -0700
Subject: [PATCH 21/24] fix(feedback): preserve screenshot detail and pending
paste text
Optimize oversized attachments only as full-resolution lossless PNG; refuse them with existing warnings when that cannot fit. Bound each pending batch independently so earlier optimization slack, rejected files, or removed drafts cannot under-reserve bytes and swallow co-pasted text. Regression tests demonstrate the old mixed-batch text loss.
---
.../sidebar/SidebarFeedbackDialog.test.tsx | 64 +++++++++
.../sidebar/use-sidebar-feedback-images.ts | 8 +-
.../lib/feedback-image-attachments.test.ts | 115 ++++++---------
.../src/lib/feedback-image-attachments.ts | 28 ++--
.../src/lib/feedback-image-shrink.test.ts | 132 +++---------------
src/renderer/src/lib/feedback-image-shrink.ts | 84 ++---------
6 files changed, 152 insertions(+), 279 deletions(-)
diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx
index 8f7d75733940..9cd8a9f3b502 100644
--- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx
+++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx
@@ -584,6 +584,70 @@ describe('SidebarFeedbackDialog image submission', () => {
expect(paste.defaultPrevented).toBe(false)
})
+ it('preserves pasted text while a mixed pending batch can fill the byte budget', async () => {
+ const user = userEvent.setup()
+ let finishFirstRead: ((value: unknown) => void) | undefined
+ mocks.readFeedbackImageFiles
+ .mockReturnValueOnce(
+ new Promise((resolve) => {
+ finishFirstRead = resolve
+ })
+ )
+ .mockResolvedValueOnce({ images: [], errors: ['No room for second.png'], notices: [] })
+ const { container } = render()
+ const textarea = screen.getByPlaceholderText('What could we improve?')
+ await waitFor(() => expect(textarea.value).toContain('Orca:'))
+ fireEvent.change(textarea, { target: { value: 'before after' } })
+ const retina = new File(['x'], 'retina.png', { type: 'image/png' })
+ const gif = new File(['x'], 'anim.gif', { type: 'image/gif' })
+ Object.defineProperty(retina, 'size', { value: 6_400_000 })
+ Object.defineProperty(gif, 'size', { value: 2_444_304 })
+ fireEvent.change(container.querySelector('input[type="file"]')!, {
+ target: { files: [retina, gif] }
+ })
+ await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledTimes(1))
+ textarea.focus()
+ textarea.setSelectionRange(7, 7)
+ const clipboard = new DataTransfer()
+ clipboard.setData('text/plain', 'report')
+ const second = new File(['x'], 'second.png', { type: 'image/png' })
+ Object.defineProperty(second, 'size', { value: 1_500_000 })
+ Object.defineProperty(clipboard, 'files', { value: [second] })
+
+ await user.paste(clipboard)
+ expect(textarea.value).toBe('before reportafter')
+ await user.keyboard('!')
+ expect(textarea.value).toBe('before report!after')
+ await act(async () =>
+ finishFirstRead?.({
+ images: [
+ {
+ id: 'retina',
+ name: 'retina.png',
+ contentType: 'image/png',
+ bytes: 1_750_000,
+ data: new Uint8Array([1]),
+ previewUrl: 'blob:retina'
+ },
+ {
+ id: 'gif',
+ name: 'anim.gif',
+ contentType: 'image/gif',
+ bytes: 2_444_304,
+ data: new Uint8Array([1]),
+ previewUrl: 'blob:gif'
+ }
+ ],
+ errors: [],
+ notices: []
+ })
+ )
+ await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledTimes(2))
+ expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(2, [second], 2, 4_194_304)
+ expect(textarea.value).toBe('before report!after')
+ expect(textarea.selectionStart).toBe(14)
+ })
+
// Why: a screenshot still shrinking will commit at most half the budget; holding
// its raw size against the gate would let a second paste spill into the textarea.
it('consumes a second oversized paste while the first is still shrinking', async () => {
diff --git a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts
index 385e80ddf20f..d377a5603e98 100644
--- a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts
+++ b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts
@@ -80,11 +80,9 @@ export function useSidebarFeedbackImages(params: {
)
return
}
- const reserved = getReservedImageCapacity()
const pendingReads = pendingImageReadsRef.current
- // Why: an oversized screenshot commits at most its shrink target, so reserving
- // its file size would turn the paste gate against the next one while it shrinks.
- const batchBytes = maxFeedbackImageBatchBytes(files, reserved.count, reserved.bytes)
+ // Why: earlier reads can fail and attached images can be removed before this reads.
+ const batchBytes = maxFeedbackImageBatchBytes(files, 0, 0)
pendingReads.count += files.length
pendingReads.bytes += batchBytes
setPendingImageReadCount((current) => current + files.length)
@@ -134,7 +132,7 @@ export function useSidebarFeedbackImages(params: {
console.error('Failed to settle a feedback image batch:', error)
})
},
- [getReservedImageCapacity, params.isSubmitting, params.mountedRef]
+ [params.isSubmitting, params.mountedRef]
)
const handleRemoveImage = useCallback((id: string) => {
diff --git a/src/renderer/src/lib/feedback-image-attachments.test.ts b/src/renderer/src/lib/feedback-image-attachments.test.ts
index 6c897c19b75a..f3dd087a6b7d 100644
--- a/src/renderer/src/lib/feedback-image-attachments.test.ts
+++ b/src/renderer/src/lib/feedback-image-attachments.test.ts
@@ -12,11 +12,6 @@ import {
maxFeedbackImageBatchBytes,
readFeedbackImageFiles
} from './feedback-image-attachments'
-import {
- feedbackImageShrinkSteps,
- shrinkFeedbackImageWithin,
- type FeedbackImageShrinkStep
-} from './feedback-image-shrink'
import type * as FeedbackImageShrinkModule from './feedback-image-shrink'
// Spelled out rather than read from the cap constant, so the policy is what is pinned.
@@ -24,7 +19,7 @@ const HALF_THE_BUDGET = MAX_FEEDBACK_IMAGE_TOTAL_BYTES / 2
const { shrinkFeedbackImage } = vi.hoisted(() => ({ shrinkFeedbackImage: vi.fn() }))
-// Why: happy-dom has no image decoder; the shrink steps are covered in their own test.
+// Why: happy-dom has no image decoder; full-resolution PNG encoding has a separate test.
vi.mock('./feedback-image-shrink', async (importOriginal) => ({
...(await importOriginal()),
shrinkFeedbackImage
@@ -103,49 +98,6 @@ function encoded(size: number, type: string): Blob {
return new Blob([new Uint8Array(size)], { type })
}
-type StepSizes = Record<`${FeedbackImageShrinkStep['contentType']}@${number}`, number>
-
-// Encode sizes measured from three 3024x1964 Retina screenshots in Electron;
-// the PNG@0.75 and other unmeasured steps of the 2nd and 3rd are estimates.
-const RETINA_STEP_SIZES: readonly StepSizes[] = [
- {
- 'image/png@0.75': 3_810_000,
- 'image/png@0.5': 1_750_000,
- 'image/jpeg@1': 1_380_000,
- 'image/jpeg@0.75': 760_000,
- 'image/jpeg@0.5': 380_000,
- 'image/jpeg@0.25': 110_000
- },
- {
- 'image/png@0.75': 3_300_000,
- 'image/png@0.5': 1_510_000,
- 'image/jpeg@1': 1_300_000,
- 'image/jpeg@0.75': 710_000,
- 'image/jpeg@0.5': 360_000,
- 'image/jpeg@0.25': 105_000
- },
- {
- 'image/png@0.75': 3_500_000,
- 'image/png@0.5': 1_630_000,
- 'image/jpeg@1': 1_340_000,
- 'image/jpeg@0.75': 730_000,
- 'image/jpeg@0.5': 370_000,
- 'image/jpeg@0.25': 108_000
- }
-]
-
-/** Runs the real step walk, with each step's encode size taken from the file's table. */
-function shrinkByStepSizes(sizesByName: Record): void {
- shrinkFeedbackImage.mockImplementation((file: File, maxBytes: number) =>
- shrinkFeedbackImageWithin(
- feedbackImageShrinkSteps(file.type),
- async (step) =>
- encoded(sizesByName[file.name][`${step.contentType}@${step.scale}`], step.contentType),
- maxBytes
- )
- )
-}
-
describe('hasAttachableFeedbackImage', () => {
it('is true when any file is an allow-listed type', () => {
const svg = new File(['x'], 'a.svg', { type: 'image/svg+xml' })
@@ -235,7 +187,28 @@ describe('maxFeedbackImageBatchBytes', () => {
}
})
- it('reserves each file in a batch against the space the ones before it reserved', () => {
+ it('covers a GIF admitted by slack from an earlier optimization', async () => {
+ shrinkFeedbackImage.mockResolvedValue(encoded(1_750_000, 'image/png'))
+ const batch = [pngFile('retina.png', 6_400_000), gifFile('anim.gif', 2_444_304)]
+
+ const reserved = maxFeedbackImageBatchBytes(batch, 0, 0)
+ const { images, errors } = await readFeedbackImageFiles(batch, 0)
+
+ expect(errors).toEqual([])
+ expect(images).toHaveLength(2)
+ expect(images.reduce((total, image) => total + image.bytes, 0)).toBe(4_194_304)
+ expect(reserved).toBeGreaterThanOrEqual(4_194_304)
+ })
+
+ it('reserves later files when an earlier rejected file can free a count slot', () => {
+ const batch = [pngFile('maybe-invalid.png', 1000), gifFile('anim.gif', 2_444_304)]
+
+ expect(
+ maxFeedbackImageBatchBytes(batch, MAX_FEEDBACK_IMAGE_COUNT - 1, 0)
+ ).toBeGreaterThanOrEqual(2_444_304)
+ })
+
+ it('caps independent file reservations at the total budget', () => {
const batch = [
pngFile('first.png', 6_400_000),
pngFile('second.png', 6_400_000),
@@ -282,11 +255,10 @@ describe('readFeedbackImageFiles', () => {
expect(errors.at(-1)).toBe('96 additional images could not be attached.')
})
- // Why: the whole budget free must not let one shrink take the PNG@0.75 step and
- // leave the next screenshot a blurry JPEG.
+ // Why: optimizing one image must still leave room for another attachment.
it('shrinks an oversized screenshot to at most half the budget even when all of it is free', async () => {
const file = pngFile('retina.png', 6_400_000)
- shrinkByStepSizes({ 'retina.png': RETINA_STEP_SIZES[0] })
+ shrinkFeedbackImage.mockResolvedValue(encoded(1_750_000, 'image/png'))
const { images, errors, notices } = await readFeedbackImageFiles([file], 0)
@@ -304,34 +276,31 @@ describe('readFeedbackImageFiles', () => {
])
})
- it('keeps three Retina screenshots added one after another readable within the budget', async () => {
- const names = ['first.png', 'second.png', 'third.png']
- shrinkByStepSizes(
- Object.fromEntries(names.map((name, index) => [name, RETINA_STEP_SIZES[index]]))
+ it('refuses an additional image when full-resolution PNG cannot fit the remaining budget', async () => {
+ shrinkFeedbackImage.mockImplementation(async (_file: File, maxBytes: number) =>
+ maxBytes >= 1_750_000 ? encoded(1_750_000, 'image/png') : null
)
- const sources = [6_400_000, 5_350_000, 5_700_000]
const attached: { contentType: string; bytes: number }[] = []
- for (const [index, name] of names.entries()) {
- const committed = attached.reduce((total, image) => total + image.bytes, 0)
+ for (const name of ['first.png', 'second.png']) {
const { images, errors } = await readFeedbackImageFiles(
- [pngFile(name, sources[index], { width: 3024, height: 1964 })],
+ [pngFile(name, 6_400_000)],
attached.length,
- committed
+ attached.reduce((total, image) => total + image.bytes, 0)
)
expect(errors).toEqual([])
attached.push(...images)
}
-
- expect(attached.map((image) => [image.contentType, image.bytes])).toEqual([
- ['image/png', 1_750_000],
- ['image/png', 1_510_000],
- ['image/jpeg', 730_000]
- ])
- expect(attached.every((image) => image.bytes <= HALF_THE_BUDGET)).toBe(true)
- expect(attached.reduce((total, image) => total + image.bytes, 0)).toBeLessThanOrEqual(
- MAX_FEEDBACK_IMAGE_TOTAL_BYTES
+ const { images, errors, notices } = await readFeedbackImageFiles(
+ [pngFile('third.png', 6_400_000)],
+ attached.length,
+ attached.reduce((total, image) => total + image.bytes, 0)
)
+
+ expect(attached.map((image) => image.contentType)).toEqual(['image/png', 'image/png'])
+ expect(images).toEqual([])
+ expect(errors).toEqual(['third.png would bring the attachments over 4.0 MB in total.'])
+ expect(notices).toEqual([])
})
// Why: the cap applies only to a re-encode; an image that fits is sent as taken.
@@ -349,7 +318,7 @@ describe('readFeedbackImageFiles', () => {
it('shrinks into the space left when less than half the budget remains', async () => {
const first = pngFile('first.png', 3_000_000)
const second = pngFile('second.png', 3_000_000)
- shrinkFeedbackImage.mockResolvedValue(encoded(900_000, 'image/jpeg'))
+ shrinkFeedbackImage.mockResolvedValue(encoded(900_000, 'image/png'))
const { images, errors } = await readFeedbackImageFiles([first, second], 0)
@@ -361,7 +330,7 @@ describe('readFeedbackImageFiles', () => {
)
expect(images.map((image) => [image.name, image.contentType, image.bytes])).toEqual([
['first.png', 'image/png', 3_000_000],
- ['second.png', 'image/jpeg', 900_000]
+ ['second.png', 'image/png', 900_000]
])
})
diff --git a/src/renderer/src/lib/feedback-image-attachments.ts b/src/renderer/src/lib/feedback-image-attachments.ts
index 13f798a926a1..59a1459d1ef5 100644
--- a/src/renderer/src/lib/feedback-image-attachments.ts
+++ b/src/renderer/src/lib/feedback-image-attachments.ts
@@ -29,10 +29,9 @@ export const FEEDBACK_IMAGE_FILE_ACCEPT = SUPPORTED_FEEDBACK_IMAGE_TYPES.join(',
const MAX_FEEDBACK_IMAGE_DETAIL_ERRORS = 4
// Why: an oversized image is read whole before it can be shrunk, so cap the read.
export const MAX_FEEDBACK_IMAGE_SOURCE_BYTES = 32 * 1024 * 1024
-// Why: a Retina screenshot's smallest step measured ~110 KB; below this, shrinks only burn encodes.
+// Why: a nearly spent budget is unlikely to fit a full-resolution PNG re-encode.
export const MIN_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES = 64 * 1024
-// Why: a shrink keeps the largest step that fits, so given the whole budget the
-// first screenshot leaves the next only a blurry JPEG and the third no room.
+// Why: reserve room for another image instead of filling the budget with one optimization.
export const MAX_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES = MAX_FEEDBACK_IMAGE_TOTAL_BYTES / 2
export type FeedbackImageDraft = {
@@ -115,30 +114,27 @@ export function hasAttachableFeedbackImage(
)
}
-/**
- * The most a batch can commit, for the paste gate to reserve while it is read:
- * each file's own size if it fits the space left, else its capped shrink target.
- */
+/** Upper bound independent of earlier files shrinking, failing, or freeing slots. */
export function maxFeedbackImageBatchBytes(
files: readonly File[],
existingCount: number,
existingBytes: number
): number {
- let remaining = MAX_FEEDBACK_IMAGE_COUNT - existingCount
- let remainingBytes = MAX_FEEDBACK_IMAGE_TOTAL_BYTES - existingBytes
+ if (existingCount >= MAX_FEEDBACK_IMAGE_COUNT) {
+ return 0
+ }
+ const remainingBytes = Math.max(0, MAX_FEEDBACK_IMAGE_TOTAL_BYTES - existingBytes)
+ const fitBytes = feedbackImageFitBytes(remainingBytes)
let batchBytes = 0
for (const file of files) {
- if (remaining <= 0) {
- break
- }
- const fitBytes = feedbackImageFitBytes(remainingBytes)
if (!isSupportedType(file.type) || file.size === 0 || !canAttachWithin(file, fitBytes)) {
continue
}
const fileBytes = file.size <= fitBytes ? file.size : feedbackImageShrinkTargetBytes(fitBytes)
- remaining -= 1
- remainingBytes -= fileBytes
batchBytes += fileBytes
+ if (batchBytes >= remainingBytes) {
+ return remainingBytes
+ }
}
return batchBytes
}
@@ -160,7 +156,7 @@ function feedbackImageDisplayName(file: File): string {
}
/**
- * Converts picked/pasted/dropped files into drafts, shrinking any that would not
+ * Converts picked/pasted/dropped files into drafts, optimizing full-resolution PNG for any that would not
* fit. Rejections and compressions come back as messages, because a silently
* dropped or degraded attachment is the exact failure this feature exists to fix.
*/
diff --git a/src/renderer/src/lib/feedback-image-shrink.test.ts b/src/renderer/src/lib/feedback-image-shrink.test.ts
index c7d0b801c13f..f00f25fa7c73 100644
--- a/src/renderer/src/lib/feedback-image-shrink.test.ts
+++ b/src/renderer/src/lib/feedback-image-shrink.test.ts
@@ -1,87 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
-import {
- feedbackImageShrinkSteps,
- shrinkFeedbackImage,
- shrinkFeedbackImageWithin,
- type FeedbackImageShrinkStep
-} from './feedback-image-shrink'
-
-function encoderBySize(sizeFor: (step: FeedbackImageShrinkStep) => number | null): {
- encode: (step: FeedbackImageShrinkStep) => Promise
- tried: FeedbackImageShrinkStep[]
-} {
- const tried: FeedbackImageShrinkStep[] = []
- return {
- tried,
- encode: async (step) => {
- tried.push(step)
- const size = sizeFor(step)
- return size === null ? null : new Blob([new Uint8Array(size)], { type: step.contentType })
- }
- }
-}
-
-const pngSteps = feedbackImageShrinkSteps('image/png')
-
-describe('feedbackImageShrinkSteps', () => {
- it('tries every PNG size before the first lossy one for a PNG source', () => {
- expect(pngSteps.map((step) => step.contentType)).toEqual([
- 'image/png',
- 'image/png',
- 'image/jpeg',
- 'image/jpeg',
- 'image/jpeg',
- 'image/jpeg'
- ])
- })
-
- // Why: a PNG of decoded JPEG noise is larger than the source it must undercut,
- // so those two full-size encodes could only ever waste time and memory.
- it('skips the PNG sizes for an already-lossy JPEG source', () => {
- expect(
- feedbackImageShrinkSteps('image/jpeg').every((step) => step.contentType === 'image/jpeg')
- ).toBe(true)
- })
-
- it('keeps every step a downscale or a same-size re-encode', () => {
- expect(pngSteps.every((step) => step.scale > 0 && step.scale <= 1)).toBe(true)
- })
-})
-
-describe('shrinkFeedbackImageWithin', () => {
- it('stops at the first encoding that fits', async () => {
- const { encode, tried } = encoderBySize((step) =>
- step.contentType === 'image/png' && step.scale === 0.5 ? 900 : 5000
- )
-
- const blob = await shrinkFeedbackImageWithin(pngSteps, encode, 1000)
-
- expect(blob?.size).toBe(900)
- expect(blob?.type).toBe('image/png')
- expect(tried.at(-1)).toEqual({ scale: 0.5, contentType: 'image/png' })
- })
-
- // Why: screenshots are mostly text, so every PNG size is tried before any JPEG.
- it('falls back to JPEG only after every PNG size is too big', async () => {
- const { encode, tried } = encoderBySize((step) =>
- step.contentType === 'image/jpeg' ? 800 : 5000
- )
-
- const blob = await shrinkFeedbackImageWithin(pngSteps, encode, 1000)
-
- expect(blob?.type).toBe('image/jpeg')
- expect(tried.map((step) => step.contentType)).toEqual(['image/png', 'image/png', 'image/jpeg'])
- })
-
- it('returns null when nothing fits or the browser cannot encode', async () => {
- expect(
- await shrinkFeedbackImageWithin(pngSteps, encoderBySize(() => 5000).encode, 1000)
- ).toBeNull()
- expect(
- await shrinkFeedbackImageWithin(pngSteps, encoderBySize(() => null).encode, 1000)
- ).toBeNull()
- })
-})
+import { shrinkFeedbackImage } from './feedback-image-shrink'
describe('shrinkFeedbackImage', () => {
type FakeCanvas = { width: number; height: number; calls: string[] }
@@ -125,33 +43,23 @@ describe('shrinkFeedbackImage', () => {
vi.unstubAllGlobals()
})
- it('decodes once, downscales each step, and releases the bitmap and every canvas', async () => {
- const { canvases, bitmap } = stubCanvas((canvas) => (canvas.width > 1000 ? 5000 : 900))
-
- const blob = await shrinkFeedbackImage(new Blob(['x'], { type: 'image/png' }), 1000)
-
- expect(blob?.type).toBe('image/png')
- expect(blob?.size).toBe(900)
- expect(createImageBitmap).toHaveBeenCalledTimes(1)
- expect(canvases.map((canvas) => canvas.calls[0])).toEqual(['draw 1500x750', 'draw 1000x500'])
- expect(canvases.every((canvas) => canvas.width === 0 && canvas.height === 0)).toBe(true)
- expect(bitmap.close).toHaveBeenCalledTimes(1)
- })
-
- // Why: JPEG has no alpha, so a transparent screenshot region would otherwise encode black.
- it('paints a white backdrop under a JPEG step and encodes at the set quality', async () => {
- const { canvases } = stubCanvas(() => 900)
-
- const blob = await shrinkFeedbackImage(new Blob(['x'], { type: 'image/jpeg' }), 1000)
-
- expect(blob?.type).toBe('image/jpeg')
- expect(canvases[0].calls).toEqual([
- 'fillStyle #fff',
- 'fillRect',
- 'draw 2000x1000',
- 'toBlob image/jpeg 0.85'
- ])
- })
+ it.each(['image/png', 'image/jpeg'])(
+ 'preserves full resolution and avoids JPEG re-encoding for %s',
+ async (sourceType) => {
+ const { canvases, bitmap } = stubCanvas(() => 900)
+
+ const blob = await shrinkFeedbackImage(new Blob(['x'], { type: sourceType }), 1000)
+
+ expect(blob?.type).toBe('image/png')
+ expect(blob?.size).toBe(900)
+ expect(createImageBitmap).toHaveBeenCalledTimes(1)
+ expect(canvases).toHaveLength(1)
+ expect(canvases[0].calls).toEqual(['draw 2000x1000', 'toBlob image/png undefined'])
+ expect(canvases[0].width).toBe(0)
+ expect(canvases[0].height).toBe(0)
+ expect(bitmap.close).toHaveBeenCalledTimes(1)
+ }
+ )
it('releases the bitmap when nothing fits', async () => {
const { canvases, bitmap } = stubCanvas(() => 5000)
@@ -159,7 +67,9 @@ describe('shrinkFeedbackImage', () => {
await expect(
shrinkFeedbackImage(new Blob(['x'], { type: 'image/png' }), 1000)
).resolves.toBeNull()
- expect(canvases).toHaveLength(feedbackImageShrinkSteps('image/png').length)
+ expect(canvases).toHaveLength(1)
+ expect(canvases[0].width).toBe(0)
+ expect(canvases[0].height).toBe(0)
expect(bitmap.close).toHaveBeenCalledTimes(1)
})
diff --git a/src/renderer/src/lib/feedback-image-shrink.ts b/src/renderer/src/lib/feedback-image-shrink.ts
index 252f281a2ba1..494611383e28 100644
--- a/src/renderer/src/lib/feedback-image-shrink.ts
+++ b/src/renderer/src/lib/feedback-image-shrink.ts
@@ -1,92 +1,28 @@
-export type FeedbackImageShrinkStep = {
- scale: number
- contentType: 'image/png' | 'image/jpeg'
-}
-
-// Why: screenshots are mostly text, which JPEG smears, so every PNG size is
-// tried before the first lossy one. Retina captures stay readable at 0.5.
-const FEEDBACK_IMAGE_PNG_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [
- { scale: 0.75, contentType: 'image/png' },
- { scale: 0.5, contentType: 'image/png' }
-]
-
-const FEEDBACK_IMAGE_JPEG_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [
- { scale: 1, contentType: 'image/jpeg' },
- { scale: 0.75, contentType: 'image/jpeg' },
- { scale: 0.5, contentType: 'image/jpeg' },
- { scale: 0.25, contentType: 'image/jpeg' }
-]
-
-const FEEDBACK_IMAGE_JPEG_QUALITY = 0.85
-
-/**
- * Why: a PNG of an already-lossy source re-encodes JPEG noise losslessly, so it
- * runs many times larger than the source it has to undercut — a multi-megapixel
- * photo would burn two full-size PNG encodes and their buffers to never fit.
- */
-export function feedbackImageShrinkSteps(sourceType: string): readonly FeedbackImageShrinkStep[] {
- return sourceType === 'image/jpeg'
- ? FEEDBACK_IMAGE_JPEG_SHRINK_STEPS
- : [...FEEDBACK_IMAGE_PNG_SHRINK_STEPS, ...FEEDBACK_IMAGE_JPEG_SHRINK_STEPS]
-}
-
-/** Returns the first step's encoding that fits, or null when none does. */
-export async function shrinkFeedbackImageWithin(
- steps: readonly FeedbackImageShrinkStep[],
- // Encodes the decoded image at one step; null when the browser cannot encode it.
- encode: (step: FeedbackImageShrinkStep) => Promise,
- maxBytes: number
-): Promise {
- for (const step of steps) {
- const blob = await encode(step)
- if (blob && blob.size <= maxBytes) {
- return blob
- }
- }
- return null
-}
-
-async function encodeBitmap(
- bitmap: ImageBitmap,
- step: FeedbackImageShrinkStep
-): Promise {
+async function encodeBitmap(bitmap: ImageBitmap): Promise {
const canvas = document.createElement('canvas')
try {
- canvas.width = Math.max(1, Math.floor(bitmap.width * step.scale))
- canvas.height = Math.max(1, Math.floor(bitmap.height * step.scale))
+ canvas.width = bitmap.width
+ canvas.height = bitmap.height
const context = canvas.getContext('2d')
if (!context) {
return null
}
- if (step.contentType === 'image/jpeg') {
- // Why: JPEG has no alpha, and transparent pixels would otherwise encode black.
- context.fillStyle = '#fff'
- context.fillRect(0, 0, canvas.width, canvas.height)
- }
- context.drawImage(bitmap, 0, 0, canvas.width, canvas.height)
- // Why: toBlob hands the encode back asynchronously; toDataURL would block the dialog on it.
- return await new Promise((resolve) =>
- canvas.toBlob(resolve, step.contentType, FEEDBACK_IMAGE_JPEG_QUALITY)
- )
+ context.drawImage(bitmap, 0, 0)
+ // Why: automatic optimization must preserve screenshot resolution and avoid JPEG loss.
+ return await new Promise((resolve) => canvas.toBlob(resolve, 'image/png'))
} finally {
- // Why: toBlob snapshots the backing store, so once it has answered the step's
- // canvas can go. Holding all six until GC runs risks tripping the renderer's
- // canvas memory budget, which hands back a blank canvas — and a blank
- // screenshot would upload without anything noticing.
+ // Why: retain the backing only until toBlob answers, avoiding accumulated canvas memory.
canvas.width = 0
canvas.height = 0
}
}
-/** Decodes once and re-encodes smaller until it fits; null when nothing fits. */
+/** Re-encodes at full resolution without JPEG loss; null when it cannot fit. */
export async function shrinkFeedbackImage(image: Blob, maxBytes: number): Promise {
const bitmap = await createImageBitmap(image)
try {
- return await shrinkFeedbackImageWithin(
- feedbackImageShrinkSteps(image.type),
- (step) => encodeBitmap(bitmap, step),
- maxBytes
- )
+ const encoded = await encodeBitmap(bitmap)
+ return encoded && encoded.size <= maxBytes ? encoded : null
} finally {
bitmap.close()
}
From bdbff1264a7a1f30e2585f597f47b6b11a40eff2 Mon Sep 17 00:00:00 2001
From: Neil <4138956+nwparker@users.noreply.github.com>
Date: Mon, 5 Oct 2026 17:24:59 -0700
Subject: [PATCH 22/24] test(journal): retain provider handle import across
main integration
---
.../agent-session-journal/journal-submission-positions.test.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts b/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts
index 2ae99a42b45b..374163786053 100644
--- a/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts
+++ b/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts
@@ -7,7 +7,6 @@ import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
-import { codexProviderHandle } from '../../../shared/agent-session-provider-handle-encoding'
import { agentSessionFailureFact } from '../../../shared/agent-session-failure'
import { agentSessionFailureWords } from '../../../shared/agent-session-failure-words'
import {
@@ -19,6 +18,7 @@ import {
type AgentJournalMessageItem,
type AgentSessionJournalIdentity
} from '../../../shared/agent-session-journal-types'
+import { codexProviderHandle } from '../../../shared/agent-session-provider-handle-encoding'
import { readAgentSessionHydrationPage } from '../agent-session-wire/agent-session-history-page'
import { createTrackedJournalOpener } from './journal-host-database-test-support'
From ed9b02d60889da4a72ca0b9650a670491eb7606f Mon Sep 17 00:00:00 2001
From: Neil <4138956+nwparker@users.noreply.github.com>
Date: Mon, 5 Oct 2026 17:24:11 -0700
Subject: [PATCH 23/24] test(startup): account for encoded Windows prompt lines
---
src/shared/startup-line-prompt-carry.test.ts | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/shared/startup-line-prompt-carry.test.ts b/src/shared/startup-line-prompt-carry.test.ts
index 4520b3635d11..fafb1ddd4672 100644
--- a/src/shared/startup-line-prompt-carry.test.ts
+++ b/src/shared/startup-line-prompt-carry.test.ts
@@ -54,6 +54,13 @@ describe('whether a launch prompt rides the typed startup line', () => {
expect(plan?.launchCommand).toContain('explain this repo')
})
+ it('carries a short Windows multi-line prompt as one encoded physical launch line', () => {
+ const { plan, promptCarried } = offer('claude', linesOf(5, 40), { platform: 'win32' })
+ expect(promptCarried).toBe(true)
+ expect(plan?.launchCommand).not.toContain('\n')
+ expect(plan?.launchCommand).toContain('`n')
+ })
+
it('carries a line of exactly the budget and refuses one byte past it', () => {
const atBudget = offer('claude', promptForClaudeLineOf(TYPED_STARTUP_LINE_PROMPT_BUDGET_BYTES))
expect(new TextEncoder().encode(atBudget.plan?.launchCommand ?? '').byteLength).toBe(
@@ -195,7 +202,7 @@ describe('on a host that cannot prove the launched agent is in front', () => {
expect(promptCarried).toBe(true)
expect(plan?.launchCommand).toContain(prompt.split('\n')[0])
// Control: the same prompt is pasted where the host can prove the agent.
- expect(offer('claude', prompt, { platform: 'win32' }).promptCarried).toBe(false)
+ expect(offer('claude', prompt, { platform: 'darwin' }).promptCarried).toBe(false)
})
})
From 725ee664516efe7a47aed217a30ea5d59a9ea47f Mon Sep 17 00:00:00 2001
From: Neil <4138956+nwparker@users.noreply.github.com>
Date: Mon, 5 Oct 2026 17:32:07 -0700
Subject: [PATCH 24/24] test(compatibility): retain the released journal parser
dependency
---
package.json | 1 +
pnpm-lock.yaml | 8 ++++++++
tests/e2e/cross-version-wire/release-checkout.ts | 1 +
3 files changed, 10 insertions(+)
diff --git a/package.json b/package.json
index 3c1979d5ff0e..915a51e4aa5d 100644
--- a/package.json
+++ b/package.json
@@ -216,6 +216,7 @@
"@sanity/diff-match-patch": "^3.2.1",
"@shadcn/lint": "~0.1.5",
"@stablyai/playwright-test": "^2.1.16",
+ "@streamparser/json": "0.0.26",
"@tailwindcss/vite": "^4.2.4",
"@tanstack/react-virtual": "^3.14.13",
"@testing-library/jest-dom": "^6.9.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fbf12ea1ae17..9f833c400ca9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -293,6 +293,9 @@ importers:
'@stablyai/playwright-test':
specifier: ^2.1.16
version: 2.1.16(@playwright/test@1.63.0)(zod@4.6.5)
+ '@streamparser/json':
+ specifier: 0.0.26
+ version: 0.0.26
'@tailwindcss/vite':
specifier: ^4.2.4
version: 4.2.4(rolldown-vite@7.3.1(@emnapi/core@1.11.2)(@emnapi/runtime@1.11.2)(@types/node@25.9.5)(esbuild@0.28.2)(jiti@2.7.0)(yaml@2.9.1))
@@ -3188,6 +3191,9 @@ packages:
'@standard-schema/spec@1.1.0':
resolution: {integrity: sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==}
+ '@streamparser/json@0.0.26':
+ resolution: {integrity: sha512-46597LNFI+MFdUnzX2QJWwmdTRdq0XVD+vVNJTtGVzIrnCuhG9pFo1OAzbNBqci8UJgk/X5KJZ6LcV+y7PTuDQ==}
+
'@swc/core-darwin-arm64@1.15.46':
resolution: {integrity: sha512-IsISIT22EfktVJrlvIpnAxG2u/A9aob9l99HMlx80x72WlFmFPk1V3UhkEzx86eJP8hw049KTFv/RISho2cq2Q==}
engines: {node: '>=10'}
@@ -10214,6 +10220,8 @@ snapshots:
'@standard-schema/spec@1.1.0': {}
+ '@streamparser/json@0.0.26': {}
+
'@swc/core-darwin-arm64@1.15.46':
optional: true
diff --git a/tests/e2e/cross-version-wire/release-checkout.ts b/tests/e2e/cross-version-wire/release-checkout.ts
index 947b6f6fb4d2..adb44c15e307 100644
--- a/tests/e2e/cross-version-wire/release-checkout.ts
+++ b/tests/e2e/cross-version-wire/release-checkout.ts
@@ -14,6 +14,7 @@ export { selectLatestStableReleaseTag }
export const REPO_ROOT = resolve(import.meta.dirname, '..', '..', '..')
const DEFAULT_CACHE_ROOT = join(REPO_ROOT, 'tests', 'e2e', '.cross-version-checkouts')
+// Released sources still import @streamparser/json; retain its pinned test-only dependency.
// Bump when extraction or the alias rewrite changes so cached trees are rebuilt.
const CHECKOUT_FORMAT = 4