Chrome DevTools for coding agents
test: detect memory leaks (#2551)
Alex Rudenkod016152503ac
diff --git a/.github/workflows/test-memory-leaks.yml b/.github/workflows/test-memory-leaks.ymlnew file mode 100644index 00000000..a10e4452--- /dev/null+++ b/.github/workflows/test-memory-leaks.yml@@ -0,0 +1,50 @@+name: Memory leak tests++permissions:+ contents: read++on:+ merge_group:+ push:+ branches:+ - main+ pull_request:++jobs:+ memory-leak-test:+ name: 'Check for memory leaks'+ runs-on: ubuntu-latest++ steps:+ - name: Check out repository+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ fetch-depth: 2+ submodules: true++ - name: Set up Node.js+ uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0+ with:+ cache: npm+ node-version: 22++ - name: Install dependencies+ shell: bash+ run: npm ci+ env:+ PUPPETEER_SKIP_DOWNLOAD: true++ - name: Install browser+ shell: bash+ run: npx puppeteer browsers install chrome++ - name: Build+ run: npm run build++ - name: Disable AppArmor+ shell: bash+ run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns++ - name: Run memory leak tests+ shell: bash+ run: npm run profile -- --threshold-percentage=1diff --git a/package.json b/package.jsonindex 27af09ef..588cdd25 100644--- a/package.json+++ b/package.json@@ -33,7 +33,9 @@"update-metrics": "node scripts/update_metrics.ts","verify-npm-package": "node scripts/verify-npm-package.js","eval": "npm run build && node scripts/eval_gemini.ts",- "count-tokens": "node scripts/count_tokens.ts"+ "count-tokens": "node scripts/count_tokens.ts",+ "profile": "npm run build && node scripts/profile/profile_mcp.ts",+ "test:memory": "npm run build && node scripts/profile/profile_mcp.ts --threshold-percentage 1"},"files": ["build/src",diff --git a/scripts/profile/profile_mcp.ts b/scripts/profile/profile_mcp.tsindex 7654e564..a8a77356 100644--- a/scripts/profile/profile_mcp.ts+++ b/scripts/profile/profile_mcp.ts@@ -28,6 +28,7 @@ import {interface ProfileOptions {iterations: number;warmupIterations: number;+ thresholdPercentage?: number;}interface ScenarioEntry {@@ -90,6 +91,7 @@ interface ScenarioResult {name: string;summary: string;isGrowth: boolean;+ exceededThreshold: boolean;}async function profileScenario(@@ -211,6 +213,7 @@ async function profileScenario(let summary = `[${scenarioName}] Unable to compute heap comparison.`;let isGrowth = false;+ let exceededThreshold = false;const baseline = measurements.find(measurement => measurement.label === 'baseline',);@@ -222,8 +225,17 @@ async function profileScenario(if (growth > 0) {isGrowth = true;const percentage = (growth / baseline.usedSize) * 100;- summary = `[${scenarioName}] The MCP heap grew by ${formatBytes(growth)} (${percentage.toFixed(2)}%) across ${options.iterations} measured iterations. Compare baseline.heapsnapshot with final.heapsnapshot to determine whether retained workload objects accumulated.`;- console.warn(summary);+ if (+ options.thresholdPercentage !== undefined &&+ percentage > options.thresholdPercentage+ ) {+ exceededThreshold = true;+ summary = `[${scenarioName}] FAIL: The MCP heap grew by ${formatBytes(growth)} (${percentage.toFixed(2)}%) across ${options.iterations} measured iterations, exceeding the threshold of ${options.thresholdPercentage}%. Compare baseline.heapsnapshot with final.heapsnapshot to determine whether retained workload objects accumulated.`;+ console.error(summary);+ } else {+ summary = `[${scenarioName}] The MCP heap grew by ${formatBytes(growth)} (${percentage.toFixed(2)}%) across ${options.iterations} measured iterations. Compare baseline.heapsnapshot with final.heapsnapshot to determine whether retained workload objects accumulated.`;+ console.warn(summary);+ }} else {summary = `[${scenarioName}] The MCP heap did not grow across the measured iterations (${formatBytes(growth)}).`;console.log(summary);@@ -237,7 +249,7 @@ async function profileScenario();console.log(`Report: ${reportPath}`);- return {name: scenarioName, summary, isGrowth};+ return {name: scenarioName, summary, isGrowth, exceededThreshold};} finally {inspector?.close();await client.close();@@ -252,6 +264,7 @@ async function main(): Promise<void> {'warmup-iterations': {type: 'string'},scenario: {type: 'string'},url: {type: 'string'},+ 'threshold-percentage': {type: 'string'},},});@@ -266,6 +279,10 @@ async function main(): Promise<void> {'--warmup-iterations',): undefined;+ const thresholdPercentage =+ values['threshold-percentage'] !== undefined+ ? Number(values['threshold-percentage'])+ : undefined;const rootDir = path.resolve(import.meta.dirname, '../..');const scenariosDir = path.join(import.meta.dirname, 'scenarios');@@ -280,6 +297,7 @@ async function main(): Promise<void> {const sourceMapTestServer = await startSourceMapTestServer();const targetUrl = values.url ?? sourceMapTestServer.url;+ const results: ScenarioResult[] = [];try {console.log(`Source map test server listening at: ${sourceMapTestServer.url}`,@@ -289,7 +307,6 @@ async function main(): Promise<void> {}console.log(`Scenarios to run: ${scenarios.map(s => s.name).join(', ')}`);- const results: ScenarioResult[] = [];for (const scenarioEntry of scenarios) {const scenarioDefaults =scenarioEntry.scenario.getNumIterations?.() ?? {};@@ -306,6 +323,7 @@ async function main(): Promise<void> {const options: ProfileOptions = {iterations,warmupIterations,+ thresholdPercentage,};const scenarioOutputDir =@@ -326,18 +344,29 @@ async function main(): Promise<void> {);results.push(result);}-- console.log('\n=== Summary ===');- for (const result of results) {- if (result.isGrowth) {- console.warn(result.summary);- } else {- console.log(result.summary);- }- }} finally {await sourceMapTestServer.close();}++ console.log('\n=== Summary ===');+ let hasFailures = false;+ for (const result of results) {+ if (result.exceededThreshold) {+ hasFailures = true;+ console.error(result.summary);+ } else if (result.isGrowth) {+ console.warn(result.summary);+ } else {+ console.log(result.summary);+ }+ }++ if (hasFailures) {+ console.error(+ '\nMemory growth threshold exceeded for one or more scenarios.',+ );+ process.exit(1);+ }}main().catch(error => {diff --git a/scripts/profile/scenarios/get_console_messages.ts b/scripts/profile/scenarios/get_console_messages.tsindex e5936084..f27199c7 100644--- a/scripts/profile/scenarios/get_console_messages.ts+++ b/scripts/profile/scenarios/get_console_messages.ts@@ -8,8 +8,8 @@ import type {ScenarioArgs, ScenarioIterations, ToolCall} from '../types.ts';export function getNumIterations(): ScenarioIterations {return {- iterations: 1,- warmupIterations: 0,+ iterations: 3,+ warmupIterations: 3,};}