~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
429 lines · 18.7 KB Raw History
// What the workspace and the server hold in memory, as a person uses HUD:
// three canvases of 8 to 10 tiles each (big feeds of posts with pictures,
// link cards and videos, a thread, profiles, galleries, documents, a
// playlist), the big feeds scrolled five pages each, every tab switched to
// twice, then 200 tool calls over MCP. After each stage it prints the
// page's JS heap, its DOM nodes and listeners, the pictures and players it
// holds, the renderer's image memory as Chromium's memory-infra counts it,
// and the server's RSS. Not part of `just e2e`.
//
//   just memory [label]    the release build, this scenario; the numbers
//                          also go to target/memory/<label>.json
//
// HUD_MEMORY_BIN runs another `hud` (a build of an earlier commit, to
// compare); HUD_MEMORY_PAGES the pages each big feed is scrolled (5);
// HUD_MEMORY_SNAPSHOT=<stage>[,…] saves a heap snapshot at those stages
// (load, scrolled, switched, tool-calls) and prints what holds most of it.
//
// The world is e2e's (world.mjs, setup.mjs with HUD_MEMORY set), reading
// public data. One thing differs from a person's use: the local PDS cannot
// vouch for alice to Bluesky's AppView, so the page's `query` calls are
// sent to HUD's /mcp as no one, which reads the same public answers
// without the PDS in between. Feeds change from one run to the next:
// compare runs made close together.

import { execFileSync } from 'node:child_process';
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { expect, signIn, test, world } from './pages.mjs';
import { root } from './world.mjs';

const BSKY = 'did:plc:z72i7hdynmk6r22z27h6tvur';
const feed = (name) => `at://${BSKY}/app.bsky.feed.generator/${name}`;
const q = (method, params, title) => ({ tool: 'query', arguments: { method, params }, title });
const rec = (uri, title) => ({ tool: 'get_record', arguments: { uri }, title });

/** The canvases, each its tiles; `big` marks the feeds scrolled. */
const CANVASES = [
	{
		name: 'Feeds',
		tiles: [
			{ ...q('app.bsky.feed.getFeed', { feed: feed('whats-hot') }, 'Discover'), big: true },
			{ ...q('app.bsky.feed.getFeed', { feed: feed('thevids') }, 'Video'), big: true },
			{ ...q('app.bsky.feed.getAuthorFeed', { actor: 'nytimes.com' }, 'NYT'), big: true },
			{ tool: 'describe_repo', arguments: { actor: 'pfrazee.com' }, title: 'pfrazee.com' },
			q('app.bsky.feed.getPostThread', { uri: 'at://did:plc:ragtjsm2j2vknwkz3zp4oxrd/app.bsky.feed.post/3mxzuknurpk2t' }, 'A thread'),
			rec('at://did:plc:bcgltzqazw5tb6k2g3ttenbj/social.grain.gallery/3mxsoldglxtk3', 'A gallery'),
			rec('at://did:plc:ragtjsm2j2vknwkz3zp4oxrd/site.standard.document/3mxzv666cxk2e', 'A document'),
			rec('at://did:plc:bcgltzqazw5tb6k2g3ttenbj/fm.plyr.list/3ma2qhay5ho2t', 'A playlist'),
			{ tool: 'list_records', arguments: { actor: 'chadtmiller.com', collection: 'app.bsky.feed.post' }, title: 'Chad’s posts' },
			q('app.bsky.feed.getAuthorFeed', { actor: 'nasa.gov' }, 'NASA'),
		],
	},
	{
		name: 'More feeds',
		tiles: [
			{ ...q('app.bsky.feed.getFeed', { feed: feed('hot-classic') }, 'What’s Hot Classic'), big: true },
			q('app.bsky.feed.getFeed', { feed: feed('bsky-team') }, 'Bluesky Team'),
			q('app.bsky.feed.getAuthorFeed', { actor: 'bsky.app' }, 'bsky.app'),
			{ tool: 'describe_repo', arguments: { actor: 'chadtmiller.com' }, title: 'chadtmiller.com' },
			{ tool: 'list_records', arguments: { actor: 'chadtmiller.com', collection: 'social.grain.gallery' }, title: 'Galleries' },
			rec('at://did:plc:bcgltzqazw5tb6k2g3ttenbj/social.grain.gallery/3mxq2wrysv5n5', 'Another gallery'),
			rec('at://did:plc:btxrwcaeyodrap5mnjw2fvmz/pub.leaflet.document/3maf67dkod22k', 'A leaflet'),
			q('app.bsky.feed.getAuthorFeed', { actor: 'theverge.com' }, 'The Verge'),
			{ tool: 'describe_collection', arguments: { actor: 'pfrazee.com', collection: 'app.bsky.feed.post' }, title: 'Posts, described' },
		],
	},
	{
		name: 'Reading',
		tiles: [
			rec('at://did:plc:q6gjnaw2blty4crticxkmujt/site.standard.document/3lzqg5b3622pn', 'Jaz'),
			rec('at://did:plc:gq4fo3u6tqzzdkjlwzpb23tj/site.standard.document/why-i-started-posting-like-its-the-2000s-again', 'Dame'),
			rec('at://did:plc:rnpkyqnmsw4ipey6eotbdnnf/site.standard.document/3mx3hwqxlfk25', 'Bailey'),
			rec('at://did:plc:bcgltzqazw5tb6k2g3ttenbj/social.grain.gallery/3mxkywszxx5tb', 'A third gallery'),
			q('app.bsky.feed.getAuthorFeed', { actor: 'pfrazee.com' }, 'Paul'),
			{ tool: 'list_records', arguments: { actor: 'chadtmiller.com', collection: 'fm.plyr.list' }, title: 'Playlists' },
			{ tool: 'describe_repo', arguments: { actor: 'bsky.app' }, title: 'bsky.app' },
			q('app.bsky.feed.getPostThread', { uri: 'at://did:plc:ragtjsm2j2vknwkz3zp4oxrd/app.bsky.feed.post/3mxzuknurpk2t' }, 'The thread again'),
		],
	},
];
/** Pages each big feed is scrolled through. */
const PAGES = Number(process.env.HUD_MEMORY_PAGES || 5);
/** Tool calls made over MCP after the page's stages. */
const CALLS = 200;

/** A tool's result over HUD's MCP as no one, or its error. */
async function mcp(name, args) {
	const res = await fetch(`${world.hudUrl}/mcp`, {
		method: 'POST',
		headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', 'mcp-protocol-version': '2025-06-18' },
		body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args } }),
	});
	const answer = await res.json();
	if (answer.error) throw new Error(JSON.stringify(answer.error));
	return answer.result;
}

/** The server's resident set, in bytes. */
const serverRss = () => Number(execFileSync('ps', ['-o', 'rss=', '-p', String(world.hudPid)]).toString().trim()) * 1024;

/** Each renderer's allocators as Chromium's memory-infra dumps them, summed over its renderers: { allocator: bytes }. */
async function memoryInfra(cdp) {
	const events = [];
	const collect = ({ value }) => events.push(...value);
	cdp.on('Tracing.dataCollected', collect);
	const done = new Promise((resolve) => cdp.once('Tracing.tracingComplete', resolve));
	await cdp.send('Tracing.start', {
		traceConfig: { includedCategories: ['disabled-by-default-memory-infra'], excludedCategories: ['*'], memoryDumpConfig: { triggers: [] } },
	});
	await cdp.send('Tracing.requestMemoryDump', { deterministic: true, levelOfDetail: 'detailed' });
	await cdp.send('Tracing.end');
	await done;
	cdp.off('Tracing.dataCollected', collect);
	const renderers = new Set(events.filter((e) => e.ph === 'M' && e.name === 'process_name' && e.args?.name === 'Renderer').map((e) => e.pid));
	const sizes = {};
	for (const e of events) {
		if (e.ph !== 'v' || !renderers.has(e.pid)) continue;
		for (const [name, dump] of Object.entries(e.args?.dumps?.allocators ?? {})) {
			const size = dump.attrs?.size?.value;
			if (size !== undefined) sizes[name] = (sizes[name] ?? 0) + parseInt(size, 16);
		}
		const footprint = e.args?.dumps?.process_totals?.private_footprint_bytes;
		if (footprint) sizes.private_footprint = (sizes.private_footprint ?? 0) + parseInt(footprint, 16);
	}
	return sizes;
}

/** What the page holds now, after a full collection. */
async function measure(page, cdp, stage) {
	await page.waitForTimeout(1500);
	await cdp.send('HeapProfiler.collectGarbage');
	await cdp.send('HeapProfiler.collectGarbage');
	const { metrics } = await cdp.send('Performance.getMetrics');
	const m = Object.fromEntries(metrics.map((x) => [x.name, x.value]));
	const dom = await cdp.send('Memory.getDOMCounters');
	const held = await page.evaluate(() => {
		const images = [...document.images];
		const loaded = images.filter((i) => i.complete && i.naturalWidth > 0);
		const media = [...document.querySelectorAll('video, audio')];
		return {
			images: images.length,
			imagesLoaded: loaded.length,
			imagePixels: loaded.reduce((sum, i) => sum + i.naturalWidth * i.naturalHeight, 0),
			// Megapixels by kind: Bluesky's CDN's by its size (avatar, feed_thumbnail…), else by host.
			byKind: Object.fromEntries(
				Object.entries(
					loaded.reduce((all, i) => {
						const u = new URL(i.currentSrc || i.src);
						const kind = u.host === 'cdn.bsky.app' ? u.pathname.split('/')[2] : u.pathname.startsWith('/blob/') ? 'blob proxy' : u.host;
						all[kind] = (all[kind] ?? 0) + i.naturalWidth * i.naturalHeight;
						return all;
					}, {}),
				).map(([k, px]) => [k, +(px / 1e6).toFixed(1)]),
			),
			media: media.length,
			mediaLoaded: media.filter((v) => v.readyState > 0).length,
			tiles: document.querySelectorAll('article[data-tile]').length,
			bodies: document.querySelectorAll('article[data-tile] .body > *').length,
		};
	});
	const infra = await memoryInfra(cdp);
	const at = (name) => infra[name] ?? 0;
	return {
		stage,
		heap: m.JSHeapUsedSize,
		heapTotal: m.JSHeapTotalSize,
		nodes: dom.nodes,
		listeners: dom.jsEventListeners,
		documents: dom.documents,
		...held,
		// Decoded pictures (cc's decode cache and discardable memory), their encoded bytes Blink keeps (web_cache), and Skia's caches.
		imageMemory: at('cc') + at('discardable') + at('web_cache') + at('skia'),
		decoded: at('cc') + at('discardable'),
		encoded: at('web_cache'),
		blinkGc: at('blink_gc'),
		partitionAlloc: at('partition_alloc'),
		rendererFootprint: at('private_footprint'),
		serverRss: serverRss(),
	};
}

/** With HUD_MEMORY_SNAPSHOT set: a heap snapshot of the page, saved, and
 * what holds most of it, by constructor (self sizes), and its detached DOM. */
async function snapshot(cdp, stage) {
	if (!(process.env.HUD_MEMORY_SNAPSHOT ?? '').split(',').includes(stage)) return;
	const out = join(root, 'target/memory');
	mkdirSync(out, { recursive: true });
	const file = join(out, `${process.env.HUD_MEMORY_LABEL || 'run'}-${stage}.heapsnapshot`);
	const parts = [];
	const take = ({ chunk }) => parts.push(Buffer.from(chunk));
	cdp.on('HeapProfiler.addHeapSnapshotChunk', take);
	await cdp.send('HeapProfiler.takeHeapSnapshot', { reportProgress: false, captureNumericValue: false });
	cdp.off('HeapProfiler.addHeapSnapshotChunk', take);
	// Too large for one string: its sections are read from the bytes.
	const bytes = Buffer.concat(parts);
	parts.length = 0;
	writeFileSync(file, bytes);
	const at = (name) => bytes.indexOf(`"${name}":`);
	const meta = JSON.parse(`${bytes.subarray(0, at('nodes')).toString().trim().replace(/,$/, '')}}`).snapshot;
	const nodes = [];
	for (let i = bytes.indexOf('[', at('nodes')) + 1, n = 0, digits = false; ; i++) {
		const c = bytes[i];
		if (c >= 48 && c <= 57) {
			n = n * 10 + c - 48;
			digits = true;
		} else {
			if (digits) nodes.push(n);
			n = 0;
			digits = false;
			if (c === 93) break;
		}
	}
	const stringsAt = bytes.indexOf('[', at('strings'));
	const strings = JSON.parse(bytes.subarray(stringsAt, bytes.lastIndexOf(']') + 1).toString());
	const fields = meta.meta.node_fields;
	const types = meta.meta.node_types[0];
	const [T, N, S, D] = ['type', 'name', 'self_size', 'detachedness'].map((f) => fields.indexOf(f));
	const by = new Map();
	let detached = 0;
	let detachedBytes = 0;
	let total = 0;
	for (let i = 0; i < nodes.length; i += fields.length) {
		const type = types[nodes[i + T]];
		const name = type === 'object' || type === 'native' || type === 'closure' ? strings[nodes[i + N]].slice(0, 60) : `(${type})`;
		const size = nodes[i + S];
		total += size;
		const row = by.get(name) ?? { count: 0, bytes: 0 };
		row.count++;
		row.bytes += size;
		by.set(name, row);
		if (D >= 0 && nodes[i + D] === 2) {
			detached++;
			detachedBytes += size;
		}
	}
	console.log(`${stage} heap snapshot: ${mb(total)} MB self size; detached ${detached} objects, ${mb(detachedBytes)} MB`);
	console.table(
		[...by.entries()]
			.sort((a, b) => b[1].bytes - a[1].bytes)
			.slice(0, 25)
			.map(([name, r]) => ({ name, count: r.count, MB: mb(r.bytes), share: `${((100 * r.bytes) / total).toFixed(1)}%` })),
	);
}

/** The workspace's XRPC procedure, called from the page as its own writes are. */
const procedure = (page, nsid, input) =>
	page.evaluate(
		async ({ nsid, input }) => {
			const res = await fetch(`/xrpc/${nsid}`, {
				method: 'POST',
				credentials: 'same-origin',
				headers: { 'Content-Type': 'application/json', 'X-HUD': '1' },
				body: JSON.stringify(input),
			});
			if (!res.ok) throw new Error(`${nsid}: ${res.status} ${await res.text()}`);
			return res.json();
		},
		{ nsid, input },
	);

/** Until the strip on screen has drawn every tile and the pictures in view have come. */
async function settled(page) {
	const strip = page.locator('main.strip.shown');
	await expect(strip.locator('.body[aria-busy="true"]')).toHaveCount(0, { timeout: 60_000 });
	await page
		.waitForFunction(
			() =>
				[...document.querySelectorAll('main.strip.shown img')].every((i) => {
					const r = i.getBoundingClientRect();
					return i.complete || r.right < 0 || r.left > innerWidth || r.bottom < 0 || r.top > innerHeight;
				}),
			null,
			{ timeout: 20_000 },
		)
		.catch(() => {});
}

const mb = (n) => (n / 1048576).toFixed(1);

test('memory', async ({ browser }) => {
	test.setTimeout(30 * 60_000);
	const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
	const page = await context.newPage();
	const cdp = await context.newCDPSession(page);
	await cdp.send('Performance.enable');

	// The page's queries, to HUD's /mcp as no one (above).
	await context.route('**/xrpc/place.hud.callTool', async (route) => {
		const body = route.request().postDataJSON();
		if (body?.name !== 'query') return route.continue();
		try {
			const result = await mcp('query', body.arguments);
			await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ result }) });
		} catch (e) {
			await route.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ error: 'InternalServerError', message: String(e) }) });
		}
	});

	await signIn(page, 'alice');
	const ws = await page.evaluate(() => fetch('/xrpc/place.hud.getWorkspace', { credentials: 'same-origin' }).then((r) => r.json()));
	const ids = [ws.current];
	for (const c of CANVASES.slice(1)) {
		const { canvas } = await procedure(page, 'place.hud.createCanvas', { name: c.name });
		await procedure(page, 'place.hud.openCanvas', { canvas: canvas.key });
		ids.push(canvas.key);
	}
	await procedure(page, 'place.hud.updateCanvas', { canvas: ids[0], name: CANVASES[0].name });
	for (const [i, c] of CANVASES.entries())
		for (const t of c.tiles) await procedure(page, 'place.hud.addTile', { canvas: ids[i], tool: t.tool, arguments: t.arguments, title: t.title });
	await procedure(page, 'place.hud.openCanvas', { canvas: ids[0] });

	const tabs = page.getByRole('tablist', { name: 'Canvases' }).getByRole('tab');
	const rows = [];
	const record = async (stage) => {
		const row = await measure(page, cdp, stage);
		rows.push(row);
		console.log(`${stage}: heap ${mb(row.heap)} MB, nodes ${row.nodes}, images ${row.imagesLoaded}/${row.images}, image memory ${mb(row.imageMemory)} MB, server ${mb(row.serverRss)} MB; image Mpx by kind ${JSON.stringify(row.byKind)}`);
		await snapshot(cdp, stage.replace(/\s+/g, '-'));
	};

	await page.reload();
	await expect(tabs).toHaveCount(CANVASES.length);
	await settled(page);
	await record('load');

	// Every big feed, five pages: scrolled down a screen at a time (so its pictures load as they pass), then Load more.
	for (const [i, c] of CANVASES.entries()) {
		if (!c.tiles.some((t) => t.big)) continue;
		await tabs.nth(i).click();
		await settled(page);
		for (const t of c.tiles.filter((t) => t.big)) {
			const tile = page.locator('main.strip.shown').getByRole('article', { name: t.title, exact: true });
			await tile.scrollIntoViewIfNeeded();
			await tile.locator('header').click({ position: { x: 4, y: 4 } });
			const body = tile.locator('.body');
			for (let p = 0; p < PAGES; p++) {
				for (let s = 0; s < 40; s++) {
					const end = await body.evaluate((el) => {
						el.scrollBy({ top: el.clientHeight * 0.9, behavior: 'instant' });
						return el.scrollTop + el.clientHeight >= el.scrollHeight - 4;
					});
					await page.waitForTimeout(120);
					if (end) break;
				}
				const more = tile.getByRole('button', { name: 'Load more' });
				if (!(await more.count())) break;
				await more.click();
				await expect(tile.getByRole('button', { name: /^Load more$|^Try again$/ })).toBeEnabled({ timeout: 30_000 }).catch(() => {});
			}
		}
	}
	await tabs.nth(0).click();
	await settled(page);
	await record('scrolled');

	// Every tab, twice round.
	for (let round = 0; round < 2; round++)
		for (let i = 0; i < CANVASES.length; i++) {
			await tabs.nth(i).click();
			await settled(page);
		}
	await tabs.nth(0).click();
	await settled(page);
	await record('switched');

	// 200 tool calls over MCP: feeds paged through, and the authors and posts they name, read every way.
	const authors = new Set();
	const posts = new Set();
	const calls = [];
	let cursor;
	for (let i = 0; i < 20; i++) {
		calls.push(['query', { method: 'app.bsky.feed.getFeed', params: { feed: feed('whats-hot') }, ...(cursor ? { cursor } : {}) }]);
		const result = await mcp(...calls.at(-1)).catch(() => null);
		const answer = result?.structuredContent?.response;
		for (const item of answer?.feed ?? []) {
			authors.add(item.post.author.did);
			posts.add(item.post.uri);
		}
		cursor = answer?.cursor;
		if (!cursor) break;
	}
	const someAuthors = [...authors];
	const somePosts = [...posts];
	for (let i = 0; calls.length < CALLS; i++) {
		const author = someAuthors[i % someAuthors.length];
		const post = somePosts[i % somePosts.length];
		const kinds = [
			['query', { method: 'app.bsky.feed.getAuthorFeed', params: { actor: author } }],
			['describe_repo', { actor: author }],
			['list_records', { actor: author, collection: 'app.bsky.feed.post' }],
			['get_record', { uri: post }],
			['query', { method: 'app.bsky.feed.getPostThread', params: { uri: post } }],
		];
		calls.push(kinds[i % kinds.length]);
	}
	const queue = calls.slice(20);
	let failed = 0;
	await Promise.all(
		Array.from({ length: 4 }, async () => {
			for (let next = queue.shift(); next; next = queue.shift()) await mcp(...next).then((r) => r.isError && failed++, () => failed++);
		}),
	);
	console.log(`${calls.length} tool calls, ${failed} failed`);
	await record('tool calls');

	console.table(
		rows.map((r) => ({
			stage: r.stage,
			'heap MB': mb(r.heap),
			nodes: r.nodes,
			listeners: r.listeners,
			'tiles (bodies)': `${r.tiles} (${r.bodies})`,
			'img loaded/all': `${r.imagesLoaded}/${r.images}`,
			'img Mpx': (r.imagePixels / 1e6).toFixed(1),
			'media loaded/all': `${r.mediaLoaded}/${r.media}`,
			'image MB': mb(r.imageMemory),
			'decoded MB': mb(r.decoded),
			'encoded MB': mb(r.encoded),
			'renderer MB': mb(r.rendererFootprint),
			'server MB': mb(r.serverRss),
		})),
	);
	const out = join(root, 'target/memory');
	mkdirSync(out, { recursive: true });
	const file = join(out, `${process.env.HUD_MEMORY_LABEL || 'run'}.json`);
	writeFileSync(file, JSON.stringify(rows, null, 2));
	console.log(`==> ${file}`);
	await context.close();
});