Skip to content

Chrome DevTools for coding agents

fix: preserve HeapSnapshotManager across browser reconnects (#3025)

Dominik Inführ64388c65c310
diff --git a/src/McpContext.ts b/src/McpContext.ts
index 942bc0d5..e9763c40 100644
--- a/src/McpContext.ts
+++ b/src/McpContext.ts
@@ -10,8 +10,8 @@ import path from 'node:path';
import {fileURLToPath, pathToFileURL} from 'node:url';
import {overrideDevToolsGlobals} from './devtools/DevtoolsUtils.js';
-import {HeapSnapshotManager} from './processors/HeapSnapshotManager.js';
import type {
+ HeapSnapshotManager,
HeapSnapshotAggregateData,
HeapSnapshotClassDiff,
HeapSnapshotDetailedClassDiff,
@@ -79,6 +79,8 @@ interface McpContextOptions {
categoryExtensions?: boolean;
// Callback when a notification should be emitted to MCP client.
onNotification?: (message: string) => void;
+ // Shared heap snapshot manager to preserve loaded snapshots across browser reconnects.
+ heapSnapshotManager: HeapSnapshotManager;
}
// Page ids are handed out from a process-wide counter so they stay unique
@@ -114,7 +116,7 @@ export class McpContext implements Context {
#locatorClass: typeof Locator;
#options: McpContextOptions;
- #heapSnapshotManager = new HeapSnapshotManager();
+ #heapSnapshotManager: HeapSnapshotManager;
#roots: Root[] | undefined = undefined;
#allowUnrestrictedPaths: boolean;
@@ -134,6 +136,7 @@ export class McpContext implements Context {
this.logger = logger;
this.#locatorClass = locatorClass;
this.#options = options;
+ this.#heapSnapshotManager = options.heapSnapshotManager;
this.#allowUnrestrictedPaths = options.allowUnrestrictedPaths ?? false;
this.#reconnectNotice = options.reconnected ?? false;
@@ -156,7 +159,6 @@ export class McpContext implements Context {
this.browser.off('targetdestroyed', this.#onTargetDestroyed);
this.#serviceWorkerConsoleCollector.dispose();
- this.#heapSnapshotManager.dispose();
for (const mcpPage of this.#mcpPages.values()) {
mcpPage.dispose();
}
@@ -878,10 +880,6 @@ export class McpContext implements Context {
return await this.#heapSnapshotManager.disposeSnapshot(filePath);
}
- hasHeapSnapshots(): boolean {
- return this.#heapSnapshotManager.hasSnapshots();
- }
-
async getHeapSnapshotRetainingPaths(
filePath: string,
nodeId: number,
diff --git a/src/index.ts b/src/index.ts
index c5d82599..d29eb18a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -12,6 +12,7 @@ import {BrowserManager} from './BrowserManager.js';
import {type ParsedArguments} from './config/ConfigParser.js';
import {loadIssueDescriptions} from './devtools/issueDescriptions.js';
import {McpContext} from './McpContext.js';
+import {HeapSnapshotManager} from './processors/HeapSnapshotManager.js';
import {ClearcutLogger} from './telemetry/ClearcutLogger.js';
import {FilePersistence} from './telemetry/persistence.js';
import {
@@ -60,6 +61,7 @@ export class McpServer {
#serverArgs: ParsedArguments;
#browserManager: BrowserManager;
#context?: McpContext;
+ #heapSnapshotManager = new HeapSnapshotManager();
/**
* Client roots stay valid across browser reconnects and only the client can
@@ -182,6 +184,7 @@ export class McpServer {
} finally {
this.#context = undefined;
}
+ this.#heapSnapshotManager.dispose();
await Promise.allSettled([
this.#browserManager.close(),
this.server.close(),
@@ -274,6 +277,7 @@ export class McpServer {
// Surfaces a one-time note in the next response after a reconnect.
reconnected: this.#context !== undefined,
categoryExtensions: this.#serverArgs.categoryExtensions,
+ heapSnapshotManager: this.#heapSnapshotManager,
onNotification: (message: string) => {
void this.server
.sendLoggingMessage({
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index 8535ebe2..cb928e21 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -17,6 +17,7 @@ import sinon from 'sinon';
import {NetworkFormatter} from '../src/formatters/NetworkFormatter.js';
import {McpContext} from '../src/McpContext.js';
import {McpPage} from '../src/McpPage.js';
+import {HeapSnapshotManager} from '../src/processors/HeapSnapshotManager.js';
import {TextSnapshot} from '../src/TextSnapshot.js';
import {type HTTPResponse} from '../src/third_party/index.js';
import type {TraceResult} from '../src/processors/PerformanceTrace.js';
@@ -355,6 +356,7 @@ describe('McpContext', () => {
const options = {
experimentalDevToolsDebugging: false,
performanceCrux: false,
+ heapSnapshotManager: new HeapSnapshotManager(),
};
const first = await McpContext.from(browser, undefined, options, Locator);
const idBeforeReconnect = (await first.newPage()).id;
@@ -380,6 +382,7 @@ describe('McpContext', () => {
);
} finally {
second.dispose();
+ options.heapSnapshotManager.dispose();
}
});
});
@@ -398,6 +401,7 @@ describe('McpContext', () => {
it('reports the reconnect notice once', async () => {
await withBrowser(async browser => {
+ const heapSnapshotManager = new HeapSnapshotManager();
const context = await McpContext.from(
browser,
undefined,
@@ -405,6 +409,7 @@ describe('McpContext', () => {
experimentalDevToolsDebugging: false,
performanceCrux: false,
reconnected: true,
+ heapSnapshotManager,
},
Locator,
);
@@ -413,28 +418,11 @@ describe('McpContext', () => {
assert.ok(!context.consumeReconnectNotice(), 'notice does not repeat');
} finally {
context.dispose();
+ heapSnapshotManager.dispose();
}
});
});
- it('disposes loaded heap snapshots on teardown', async () => {
- await withMcpContext(async (_response, context) => {
- const filePath = path.join(
- process.cwd(),
- 'tests/fixtures/example.heapsnapshot',
- );
- await context.getHeapSnapshotStats(filePath);
- assert.ok(context.hasHeapSnapshots(), 'snapshot loaded before teardown');
-
- context.dispose();
-
- assert.ok(
- !context.hasHeapSnapshots(),
- 'heap snapshots freed on teardown',
- );
- });
- });
-
it('should include network requests in structured content', async t => {
await withMcpContext(async (response, context) => {
const mockRequest = getMockRequest({
@@ -785,6 +773,7 @@ describe('McpContext', () => {
{
experimentalDevToolsDebugging: false,
performanceCrux: false,
+ heapSnapshotManager: new HeapSnapshotManager(),
...options,
},
Locator,
diff --git a/tests/roots.test.ts b/tests/roots.test.ts
index 3301c40c..7d02bdcd 100644
--- a/tests/roots.test.ts
+++ b/tests/roots.test.ts
@@ -14,6 +14,7 @@ import {pathToFileURL} from 'node:url';
import sinon from 'sinon';
import {McpContext} from '../src/McpContext.js';
+import {HeapSnapshotManager} from '../src/processors/HeapSnapshotManager.js';
import {resolveCanonicalPath} from '../src/utils/files.js';
import {escapeForLog} from '../src/utils/logger.js';
@@ -172,6 +173,7 @@ describe('McpContext path validation escaping', () => {
return await McpContext.from(browser, undefined, {
experimentalDevToolsDebugging: false,
performanceCrux: false,
+ heapSnapshotManager: new HeapSnapshotManager(),
});
}
diff --git a/tests/utils.ts b/tests/utils.ts
index e9c93377..614ddc56 100644
--- a/tests/utils.ts
+++ b/tests/utils.ts
@@ -27,6 +27,7 @@ import sinon from 'sinon';
import type {ParsedArguments} from '../src/config/ConfigParser.js';
import {McpContext} from '../src/McpContext.js';
import {McpResponse} from '../src/McpResponse.js';
+import {HeapSnapshotManager} from '../src/processors/HeapSnapshotManager.js';
import {TextSnapshot} from '../src/TextSnapshot.js';
import {DevTools} from '../src/third_party/index.js';
import {stableIdSymbol} from '../src/utils/id.js';
@@ -189,6 +190,7 @@ export async function withMcpContext(
McpContext.resetWorkerIdsForTesting();
const parsedArgs = createMockParsedArguments(args);
const response = new McpResponse(parsedArgs);
+ const heapSnapshotManager = new HeapSnapshotManager();
if (context) {
context.dispose();
}
@@ -206,6 +208,7 @@ export async function withMcpContext(
options.navigationTimeout ??
(process.platform === 'win32' ? 20000 : undefined),
categoryExtensions: parsedArgs.categoryExtensions,
+ heapSnapshotManager,
},
Locator,
);
@@ -216,6 +219,7 @@ export async function withMcpContext(
await cb(response, context, parsedArgs);
} finally {
context.dispose();
+ heapSnapshotManager.dispose();
context = undefined;
}
}, options);