Skip to content

Commit 7b508a6

Browse files
fix(server): snapshots of large ARIA trees keep a prefix of the page text
preview_snapshot halved visibleText all the way to zero before it shrank the accessibility tree, so a text page with a large tree (any table of ~100+ rows) returned visibleText "…" while leaving the byte budget partly unused. The shed loop now brings visibleText and then the tree down to a 2,000-character floor before either drops below it. Below the floor the previous order still applies (visibleText, interactiveElements, then the tree), so the byte ceiling still holds for pathological input. Fixes #33743 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bfe4758 commit 7b508a6

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
256256
export const MAX_SNAPSHOT_TEXT_BYTES = 20_000;
257257
const 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;
258260
const MAX_SNAPSHOT_ELEMENT_NAME_CHARS = 200;
259261
const MAX_SNAPSHOT_LOG_ENTRIES = 40;
260262
const 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+
301308
const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
302309
const omitted: Array<string> = [];
303310
const { accessibilityTree, ...withoutTree } = metadata;
@@ -375,18 +382,22 @@ const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
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

Comments
 (0)