Skip to content

Chrome DevTools for coding agents

fix(performance): error when performance_stop_trace is called on a non-recording page (#2971) (#3117)

Yuri (IURII) Trukhin1ab630aecc55
diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts
index 85286788..dde134f7 100644
--- a/src/tools/ToolDefinition.ts
+++ b/src/tools/ToolDefinition.ts
@@ -365,6 +365,7 @@ export type Context = Readonly<{
recordedTraces(): TraceResult[];
storeTraceRecording(result: TraceResult): void;
getPageById(pageId: number): ContextPage;
+ getPages(): ContextPage[];
newPage(
background?: boolean,
isolatedContextName?: string,
@@ -529,6 +530,7 @@ export type ContextPage = Readonly<{
isClosed(): boolean;
getScreenRecorder(): ScreenRecorderData | null;
setScreenRecorder(data: ScreenRecorderData | null): void;
+ isRunningPerformanceTrace(): boolean;
setIsRunningPerformanceTrace(x: boolean): void;
waitForEventsAfterAction(
action: (signal: AbortSignal) => Promise<unknown>,
diff --git a/src/tools/performance.ts b/src/tools/performance.ts
index 7d07a485..35b4cf82 100644
--- a/src/tools/performance.ts
+++ b/src/tools/performance.ts
@@ -6,6 +6,7 @@
import zlib from 'node:zlib';
+import type {ParsedArguments} from '../config/mcp-options.js';
import {zod, DevTools} from '../third_party/index.js';
import {
parseRawTraceBuffer,
@@ -148,38 +149,55 @@ export const startTrace: PageToolFactory = definePageTool(() => ({
},
}));
-export const stopTrace: PageToolFactory = definePageTool(() => ({
- name: 'performance_stop_trace',
- description:
- 'Stop the active performance trace recording on the target webpage.',
- annotations: {
- category: ToolCategory.PERFORMANCE,
- readOnlyHint: false,
- },
- schema: {
- filePath: filePathSchema,
- },
- blockedByDialog: true,
- concurrency: 'exclusive',
- verifyFilesSchema: {
- filePath: true,
- },
- handler: async (request, response, context) => {
- if (!context.isRunningPerformanceTrace()) {
- response.appendResponseLine(
- 'Error: no active performance trace recording to stop.',
+export const stopTrace: PageToolFactory = definePageTool(
+ (args: ParsedArguments) => ({
+ name: 'performance_stop_trace',
+ description:
+ 'Stop the active performance trace recording on the target webpage.',
+ annotations: {
+ category: ToolCategory.PERFORMANCE,
+ readOnlyHint: false,
+ },
+ schema: {
+ filePath: filePathSchema,
+ },
+ blockedByDialog: true,
+ concurrency: 'exclusive',
+ verifyFilesSchema: {
+ filePath: true,
+ },
+ handler: async (request, response, context) => {
+ if (!context.isRunningPerformanceTrace()) {
+ response.appendResponseLine(
+ 'Error: no active performance trace recording to stop.',
+ );
+ return;
+ }
+ const page = request.page;
+ if (!page.isRunningPerformanceTrace()) {
+ const tracedPage = context
+ .getPages()
+ .find(p => p.isRunningPerformanceTrace());
+ const pageHint = tracedPage ? ` on page ${tracedPage.id}` : '';
+ const actionHint = tracedPage
+ ? args.pageIdRouting
+ ? `Pass pageId: ${tracedPage.id} to performance_stop_trace to stop it.`
+ : `Select page ${tracedPage.id} with select_page first, then call performance_stop_trace.`
+ : 'Select the page that started the trace and try again.';
+ response.appendResponseLine(
+ `Error: the active performance trace is running${pageHint}, not on page ${page.id}. ${actionHint}`,
+ );
+ return;
+ }
+ await stopTracingAndAppendOutput(
+ page,
+ response,
+ context,
+ request.params.filePath,
);
- return;
- }
- const page = request.page;
- await stopTracingAndAppendOutput(
- page,
- response,
- context,
- request.params.filePath,
- );
- },
-}));
+ },
+ }),
+);
export const analyzeInsight: PageToolFactory = definePageTool(() => ({
name: 'performance_analyze_insight',
diff --git a/tests/mocks.ts b/tests/mocks.ts
index 3ae1ca65..1bf96c12 100644
--- a/tests/mocks.ts
+++ b/tests/mocks.ts
@@ -562,6 +562,7 @@ export function createMockMcpContext(
const context = sinon.createStubInstance(McpContext);
const page = options.selectedPage ?? createMockMcpPage();
context.getSelectedMcpPage.returns(page satisfies McpPage);
+ context.getPages.returns([page]);
context.getTargetInfos.resolves(new Map());
// Real queues, so handler tests observe the real scheduling.
const taskRunner = options.taskRunner ?? new TaskRunner();
diff --git a/tests/tools/performance.test.ts b/tests/tools/performance.test.ts
index 665c6c11..2e49def1 100644
--- a/tests/tools/performance.test.ts
+++ b/tests/tools/performance.test.ts
@@ -397,6 +397,54 @@ describe('performance', () => {
);
});
+ it('rejects stopping a trace on a page that is not recording and suggests select_page by default', async () => {
+ const {page, context, response, args} = createHandlerMocks({
+ pageIdRouting: false,
+ });
+ Object.defineProperty(page, 'id', {value: 2});
+ page.isRunningPerformanceTrace.returns(false);
+
+ const {page: tracedPage} = createHandlerMocks();
+ Object.defineProperty(tracedPage, 'id', {value: 1});
+ tracedPage.isRunningPerformanceTrace.returns(true);
+
+ context.isRunningPerformanceTrace.returns(true);
+ context.getPages.returns([tracedPage, page]);
+
+ await stopTrace(args).handler({params: {}, page}, response, context);
+
+ sinon.assert.notCalled(page.pptrPage.tracing.stop);
+ sinon.assert.notCalled(tracedPage.pptrPage.tracing.stop);
+ sinon.assert.calledOnceWithExactly(
+ response.appendResponseLine,
+ 'Error: the active performance trace is running on page 1, not on page 2. Select page 1 with select_page first, then call performance_stop_trace.',
+ );
+ });
+
+ it('rejects stopping a trace on a page that is not recording and suggests pageId when pageIdRouting is enabled', async () => {
+ const {page, context, response, args} = createHandlerMocks({
+ pageIdRouting: true,
+ });
+ Object.defineProperty(page, 'id', {value: 2});
+ page.isRunningPerformanceTrace.returns(false);
+
+ const {page: tracedPage} = createHandlerMocks();
+ Object.defineProperty(tracedPage, 'id', {value: 1});
+ tracedPage.isRunningPerformanceTrace.returns(true);
+
+ context.isRunningPerformanceTrace.returns(true);
+ context.getPages.returns([tracedPage, page]);
+
+ await stopTrace(args).handler({params: {}, page}, response, context);
+
+ sinon.assert.notCalled(page.pptrPage.tracing.stop);
+ sinon.assert.notCalled(tracedPage.pptrPage.tracing.stop);
+ sinon.assert.calledOnceWithExactly(
+ response.appendResponseLine,
+ 'Error: the active performance trace is running on page 1, not on page 2. Pass pageId: 1 to performance_stop_trace to stop it.',
+ );
+ });
+
it('will stop the trace and return trace info when a trace is running', async () => {
const rawData = loadTraceAsBuffer('basic-trace.json.gz');
await withMcpContext(async (response, context, args) => {