Skip to content

Commit 950c342

Browse files
fix(server): preview snapshots keep page text beside large accessibility trees (#33745)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b0f12e1 commit 950c342

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,21 @@ it.effect("bounds large ARIA trees without dropping every server locator", () =>
688688
{ length: 2_000 },
689689
(_, index) => `- button "删除 ${index}" [ref=t3-snapshot-e${index}]`,
690690
).join("\n");
691-
yield* serveSnapshots("mcp-large-server-refs", { ...snapshotResult, accessibilityTree });
691+
const visibleText = Array.from({ length: 700 }, (_, index) => `Row ${index} value`).join(
692+
"\n",
693+
);
694+
yield* serveSnapshots("mcp-large-server-refs", {
695+
...snapshotResult,
696+
visibleText,
697+
accessibilityTree,
698+
});
692699
const result = yield* callSnapshot({ includeImage: false });
693700
expect(result.isError).toBe(false);
694-
const metadata = result.structuredContent as { accessibilityTree: string };
701+
const metadata = result.structuredContent as {
702+
accessibilityTree: string;
703+
visibleText: string;
704+
};
705+
expect(metadata.visibleText.slice(0, 1_000)).toBe(visibleText.slice(0, 1_000));
695706
expect(metadata.accessibilityTree).toContain("[ref=t3-snapshot-e0]");
696707
expect(metadata.accessibilityTree.length).toBeLessThan(accessibilityTree.length);
697708
const body = result.content[1];

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

Lines changed: 20 additions & 9 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;
@@ -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

Comments
 (0)