@@ -255,6 +255,8 @@ const layerMcpAuthMiddleware = HttpRouter.middleware<{
255255/** Claude Code spills oversized results to files, losing locators; bound both text and its preferred structuredContent. */
256256export const MAX_SNAPSHOT_TEXT_BYTES = 20_000 ;
257257const MAX_SNAPSHOT_VISIBLE_TEXT_CHARS = 8_000 ;
258+ // Page text and the ARIA tree both shrink to this before either drops below it.
259+ const SNAPSHOT_TEXT_FLOOR_CHARS = 2_000 ;
258260const MAX_SNAPSHOT_ELEMENT_NAME_CHARS = 200 ;
259261const MAX_SNAPSHOT_LOG_ENTRIES = 40 ;
260262const MAX_SNAPSHOT_LOG_TEXT_CHARS = 500 ;
@@ -298,6 +300,11 @@ type SnapshotMetadata = {
298300 readonly [ key : string ] : unknown ;
299301} ;
300302
303+ const halveToFloor = ( chars : number ) =>
304+ chars > SNAPSHOT_TEXT_FLOOR_CHARS
305+ ? Math . max ( SNAPSHOT_TEXT_FLOOR_CHARS , Math . floor ( chars / 2 ) )
306+ : Math . floor ( chars / 2 ) ;
307+
301308const boundSnapshotMetadata = ( metadata : SnapshotMetadata ) => {
302309 const omitted : Array < string > = [ ] ;
303310 const { accessibilityTree, ...withoutTree } = metadata ;
@@ -370,23 +377,27 @@ const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
370377 } ) ;
371378 let text = encodeJsonText ( value ( ) ) ;
372379 while ( utf8Length ( text ) > MAX_SNAPSHOT_TEXT_BYTES ) {
373- // Elements carry the locators, so they go last; logs shed newest- last.
380+ // Logs shed first, oldest first. Below the floors, locator-bearing elements and tree go last.
374381 const key =
375382 shedOrder . find (
376383 ( candidate ) => candidate !== "interactiveElements" && lists [ candidate ] . length > 0 ,
377384 ) ??
378- ( visibleTextChars > 0
385+ ( visibleTextChars > SNAPSHOT_TEXT_FLOOR_CHARS
379386 ? "visibleText"
380- : lists . interactiveElements . length > 0
381- ? "interactiveElements"
382- : ariaTreeChars > 0
383- ? "accessibilityTree"
384- : undefined ) ;
387+ : ariaTreeChars > SNAPSHOT_TEXT_FLOOR_CHARS
388+ ? "accessibilityTree"
389+ : visibleTextChars > 0
390+ ? "visibleText"
391+ : lists . interactiveElements . length > 0
392+ ? "interactiveElements"
393+ : ariaTreeChars > 0
394+ ? "accessibilityTree"
395+ : undefined ) ;
385396 if ( key === undefined ) break ;
386397 if ( key === "visibleText" ) {
387- visibleTextChars = Math . floor ( visibleTextChars / 2 ) ;
398+ visibleTextChars = halveToFloor ( visibleTextChars ) ;
388399 } else if ( key === "accessibilityTree" ) {
389- ariaTreeChars = Math . floor ( ariaTreeChars / 2 ) ;
400+ ariaTreeChars = halveToFloor ( ariaTreeChars ) ;
390401 } else {
391402 const keep = Math . floor ( lists [ key ] . length / 2 ) ;
392403 dropped [ key ] += lists [ key ] . length - keep ;
0 commit comments