The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 23:30 UTC
fix(server): preview snapshots keep page text beside large accessibility trees (#33745)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 10, 2026 at 23:27 UTC
950c34226cc08ca4d9fc0e387b059a30463a1e47
2 files changed
+33
-11
apps/server/src/mcp/McpHttpServer.test.ts
+13
-2
@@ -688,10 +688,21 @@ it.effect("bounds large ARIA trees without dropping every server locator", () =>
688
{ length: 2_000 },
689
(_, index) => `- button "删除 ${index}" [ref=t3-snapshot-e${index}]`,
690
).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
+ });
699
const result = yield* callSnapshot({ includeImage: false });
700
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));
706
expect(metadata.accessibilityTree).toContain("[ref=t3-snapshot-e0]");
707
expect(metadata.accessibilityTree.length).toBeLessThan(accessibilityTree.length);
708
const body = result.content[1];
apps/server/src/mcp/McpHttpServer.ts
+20
-9
@@ -255,6 +255,8 @@ const layerMcpAuthMiddleware = HttpRouter.middleware<{
255
/** Claude Code spills oversized results to files, losing locators; bound both text and its preferred structuredContent. */
256
export const MAX_SNAPSHOT_TEXT_BYTES = 20_000;
257
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;
260
const MAX_SNAPSHOT_ELEMENT_NAME_CHARS = 200;
261
const MAX_SNAPSHOT_LOG_ENTRIES = 40;
262
const MAX_SNAPSHOT_LOG_TEXT_CHARS = 500;
@@ -298,6 +300,11 @@ type SnapshotMetadata = {
300
readonly [key: string]: unknown;
301
};
302
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
+
308
const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
309
const omitted: Array<string> = [];
310
const { accessibilityTree, ...withoutTree } = metadata;
@@ -370,23 +377,27 @@ const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
377
});
378
let text = encodeJsonText(value());
379
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.
381
const key =
382
shedOrder.find(
383
(candidate) => candidate !== "interactiveElements" && lists[candidate].length > 0,
384
) ??
378
- (visibleTextChars > 0
385
+ (visibleTextChars > SNAPSHOT_TEXT_FLOOR_CHARS
386
? "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);
396
if (key === undefined) break;
397
if (key === "visibleText") {
387
- visibleTextChars = Math.floor(visibleTextChars / 2);
398
+ visibleTextChars = halveToFloor(visibleTextChars);
399
} else if (key === "accessibilityTree") {
389
- ariaTreeChars = Math.floor(ariaTreeChars / 2);
400
+ ariaTreeChars = halveToFloor(ariaTreeChars);
401
} else {
402
const keep = Math.floor(lists[key].length / 2);
403
dropped[key] += lists[key].length - keep;