@@ -40,6 +40,9 @@ export const CURSOR_VISION_MAX_EDGE = 2000;
4040
4141const CURSOR_VISION_JPEG_QUALITIES_DEFAULT = [ 85 , 70 , 55 , 40 ] as const ;
4242const CURSOR_VISION_JPEG_QUALITIES_HIGH = [ 90 , 80 , 65 , 50 ] as const ;
43+ /** Stop shrinking below this longest edge when chasing the soft byte cap. */
44+ const CURSOR_VISION_SOFT_MIN_EDGE = 256 ;
45+ const CURSOR_VISION_SOFT_SHRINK = 0.85 ;
4346
4447const CURSOR_VISION_PASSTHROUGH_MIME = new Set ( [
4548 "image/jpeg" ,
@@ -326,12 +329,17 @@ export function cursorImageAttachmentPath(uuid: string, mimeType: string): strin
326329 * Re-encode toward a JPEG under the soft vision cap when Bun can decode the payload.
327330 * Unsupported MIME or undecodable bytes are omitted (fail-closed) except for tiny
328331 * undecodable PNG/JPEG/GIF/WebP stubs used in unit tests (pass-through under soft cap).
332+ * After the quality ladder, edges shrink iteratively until the soft byte cap is met
333+ * (or the min edge floor is hit) so large clipboard PNGs do not leave >softMax JPEGs
334+ * that Cursor vision hallucinates on.
329335 */
330336export async function prepareCursorImageForWire (
331337 image : ResolvedCursorImage ,
332338) : Promise < PrepareCursorImageOutcome > {
333339 const mime = image . mimeType . toLowerCase ( ) ;
334340 const softMax = softMaxBytesForDetail ( image . detail ) ;
341+ const qualities = jpegQualitiesForDetail ( image . detail ) ;
342+ const lowestQuality = qualities [ qualities . length - 1 ] ! ;
335343
336344 if ( ! CURSOR_VISION_PASSTHROUGH_MIME . has ( mime ) ) {
337345 return { status : "omitted" , reason : CURSOR_VISION_IMAGE_OMITTED } ;
@@ -357,28 +365,61 @@ export async function prepareCursorImageForWire(
357365 targetH = Math . max ( 1 , Math . round ( height * scale ) ) ;
358366 }
359367
360- let best : Uint8Array | undefined ;
361- for ( const quality of jpegQualitiesForDetail ( image . detail ) ) {
368+ const encodeAt = async ( w : number , h : number , quality : number ) : Promise < Uint8Array > => {
362369 let pipeline = new Bun . Image ( image . data ) ;
363- if ( targetW > 0 && targetH > 0 && ( targetW !== width || targetH !== height ) ) {
364- pipeline = pipeline . resize ( targetW , targetH ) ;
370+ if ( w > 0 && h > 0 && ( w !== width || h !== height ) ) {
371+ pipeline = pipeline . resize ( w , h ) ;
372+ }
373+ return new Uint8Array ( await pipeline . jpeg ( { quality } ) . bytes ( ) ) ;
374+ } ;
375+
376+ let best : Uint8Array | undefined ;
377+ for ( const quality of qualities ) {
378+ const encoded = await encodeAt ( targetW , targetH , quality ) ;
379+ if ( ! best || encoded . byteLength < best . byteLength ) best = encoded ;
380+ if ( encoded . byteLength <= softMax ) {
381+ return {
382+ status : "ready" ,
383+ image : { ...image , data : encoded , mimeType : "image/jpeg" } ,
384+ } ;
385+ }
386+ }
387+
388+ // Quality ladder missed the soft cap — shrink edges until it fits or we hit the floor.
389+ while (
390+ best
391+ && best . byteLength > softMax
392+ && targetW > 0
393+ && targetH > 0
394+ && Math . max ( targetW , targetH ) > CURSOR_VISION_SOFT_MIN_EDGE
395+ ) {
396+ const nextW = Math . max ( 1 , Math . round ( targetW * CURSOR_VISION_SOFT_SHRINK ) ) ;
397+ const nextH = Math . max ( 1 , Math . round ( targetH * CURSOR_VISION_SOFT_SHRINK ) ) ;
398+ if ( Math . max ( nextW , nextH ) < CURSOR_VISION_SOFT_MIN_EDGE ) {
399+ const scale = CURSOR_VISION_SOFT_MIN_EDGE / Math . max ( targetW , targetH ) ;
400+ targetW = Math . max ( 1 , Math . round ( targetW * scale ) ) ;
401+ targetH = Math . max ( 1 , Math . round ( targetH * scale ) ) ;
402+ } else {
403+ targetW = nextW ;
404+ targetH = nextH ;
365405 }
366- const encoded = new Uint8Array ( await pipeline . jpeg ( { quality } ) . bytes ( ) ) ;
406+ const encoded = await encodeAt ( targetW , targetH , lowestQuality ) ;
367407 if ( ! best || encoded . byteLength < best . byteLength ) best = encoded ;
368408 if ( encoded . byteLength <= softMax ) {
369409 return {
370410 status : "ready" ,
371411 image : { ...image , data : encoded , mimeType : "image/jpeg" } ,
372412 } ;
373413 }
414+ if ( Math . max ( targetW , targetH ) <= CURSOR_VISION_SOFT_MIN_EDGE ) break ;
374415 }
375- if ( best && best . byteLength < image . data . byteLength ) {
416+
417+ if ( best ) {
376418 return {
377419 status : "ready" ,
378420 image : { ...image , data : best , mimeType : "image/jpeg" } ,
379421 } ;
380422 }
381- // Decoded but could not shrink under soft max — still under wire hard cap check later.
382423 return { status : "ready" , image } ;
383424 } catch {
384425 // Tiny undecodable stubs (unit wire tests) may still be referenced as blobIdWithData.
@@ -487,11 +528,12 @@ export function buildSelectedContext(
487528export function extractTrailingToolResultImagePromotion (
488529 messages : readonly OcxMessage [ ] ,
489530) : { parts : CursorImagePartRef [ ] ; omittedOlder : number ; promotedCallIds : Set < string > } {
490- let start = messages . length ;
491- while ( start > 0 && messages [ start - 1 ] ?. role === "toolResult" ) start -= 1 ;
531+ const effective = stripTrailingTransparentDeveloperMessages ( messages ) ;
532+ let start = effective . length ;
533+ while ( start > 0 && effective [ start - 1 ] ?. role === "toolResult" ) start -= 1 ;
492534
493535 const entries : Array < { callId : string ; part : CursorImagePartRef } > = [ ] ;
494- for ( const message of messages . slice ( start ) ) {
536+ for ( const message of effective . slice ( start ) ) {
495537 if ( message . role !== "toolResult" ) continue ;
496538 for ( const part of extractCursorImageParts ( message . content ) ) {
497539 entries . push ( { callId : message . toolCallId , part } ) ;
@@ -519,27 +561,61 @@ export function extractTrailingToolResultImageParts(
519561 return { parts, omittedOlder } ;
520562}
521563
564+ /**
565+ * Trailing non-image developer messages (Codex Desktop multi-agent guidance) are
566+ * transparent for Cursor vision / tool-continuation detection so `view_image`
567+ * still promotes onto SelectedImage.
568+ */
569+ export function isTransparentCursorVisionSuffix ( message : OcxMessage ) : boolean {
570+ if ( message . role !== "developer" ) return false ;
571+ return extractCursorImageParts ( message . content ) . length === 0 ;
572+ }
573+
574+ /** Drop trailing transparent developers; returns the same array reference when unchanged. */
575+ export function stripTrailingTransparentDeveloperMessages (
576+ messages : readonly OcxMessage [ ] ,
577+ ) : readonly OcxMessage [ ] {
578+ let end = messages . length ;
579+ while ( end > 0 ) {
580+ const message = messages [ end - 1 ] ;
581+ if ( ! message || ! isTransparentCursorVisionSuffix ( message ) ) break ;
582+ end -= 1 ;
583+ }
584+ return end === messages . length ? messages : messages . slice ( 0 , end ) ;
585+ }
586+
587+ /** True when, ignoring transparent developer suffix, the active turn is a toolResult. */
588+ export function cursorIsTrailingToolResultContinuation (
589+ messages : readonly OcxMessage [ ] | undefined ,
590+ ) : boolean {
591+ if ( ! messages ?. length ) return false ;
592+ return stripTrailingTransparentDeveloperMessages ( messages ) . at ( - 1 ) ?. role === "toolResult" ;
593+ }
594+
522595/**
523596 * Resolve images for the active Cursor turn.
524597 * - Trailing user/developer: attach images (SelectedImage).
525598 * - Trailing toolResult run (e.g. Codex `view_image`): same channel — McpImageContent
526599 * alone is not consumed as vision for external Cursor models like grok-4.5.
600+ * - Trailing non-image developer injections (multi_agent_mode) are stripped first so
601+ * Desktop collab guidance does not hide a view_image promotion.
527602 */
528603export async function resolveActiveCursorImages (
529604 messages : readonly OcxMessage [ ] | undefined ,
530605 signal ?: AbortSignal ,
531606) : Promise < ResolvedCursorImage [ ] > {
532607 if ( ! messages ?. length ) return [ ] ;
533608
534- const last = messages . at ( - 1 ) ;
609+ const effective = stripTrailingTransparentDeveloperMessages ( messages ) ;
610+ const last = effective . at ( - 1 ) ;
535611 if ( last ?. role === "toolResult" ) {
536- const { parts } = extractTrailingToolResultImageParts ( messages ) ;
612+ const { parts } = extractTrailingToolResultImageParts ( effective ) ;
537613 if ( parts . length === 0 ) return [ ] ;
538614 return resolveCursorImageParts ( parts , signal ) ;
539615 }
540616
541- for ( let i = messages . length - 1 ; i >= 0 ; i -- ) {
542- const message = messages [ i ] ;
617+ for ( let i = effective . length - 1 ; i >= 0 ; i -- ) {
618+ const message = effective [ i ] ;
543619 if ( ! message ) continue ;
544620 if ( message . role === "user" || message . role === "developer" ) {
545621 return resolveCursorImageParts ( extractCursorImageParts ( message . content ) , signal ) ;
0 commit comments