Chrome DevTools for coding agents
chore: add a note about snapshot regeneration (#2884)
Alex Rudenko648a6676e7ed
diff --git a/src/McpResponse.ts b/src/McpResponse.tsindex 3a9ff611..6223c23c 100644--- a/src/McpResponse.ts+++ b/src/McpResponse.ts@@ -725,13 +725,20 @@ export class McpResponse implements Response {);}- async #handleComments(): Promise<CommentFormatter | undefined> {+ async #handleComments(): Promise<+ | {+ formatter: CommentFormatter;+ commentsSnapshotRegenerated?: 'standard' | 'verbose';+ }+ | undefined+ > {const comments = this.#devToolsComments;if (!comments) {return undefined;}const page = this.#page;- return await CommentFormatter.from(comments, {+ const initialSnapshot = page?.textSnapshot;+ const formatter = await CommentFormatter.from(comments, {resolveBackendNodeId: page? (id: number) => page.resolveBackendNodeId(id): undefined,@@ -739,6 +746,13 @@ export class McpResponse implements Response {? (id: string) => page.resolveCdpRequestId(id): undefined,});+ let commentsSnapshotRegenerated: 'standard' | 'verbose' | undefined;+ if (page?.textSnapshot && page.textSnapshot !== initialSnapshot) {+ commentsSnapshotRegenerated = page.textSnapshot.verbose+ ? 'verbose'+ : 'standard';+ }+ return {formatter, commentsSnapshotRegenerated};}async handle(@@ -756,7 +770,7 @@ export class McpResponse implements Response {webmcpTools,consoleMessages,networkRequests,- comments,+ commentsResult,] = await Promise.all([this.#handleSnapshot(context),this.#handleAttachedNetworkRequest(context),@@ -791,7 +805,9 @@ export class McpResponse implements Response {lighthouseResult: this.#attachedLighthouseResult,thirdPartyDeveloperTools,webmcpTools,- comments,+ comments: commentsResult?.formatter,+ commentsSnapshotRegenerated:+ commentsResult?.commentsSnapshotRegenerated,errorMessage: this.#error?.message,},dataFormat,@@ -823,6 +839,7 @@ export class McpResponse implements Response {thirdPartyDeveloperTools?: ToolGroups;webmcpTools?: WebMCPTool[];comments?: CommentFormatter;+ commentsSnapshotRegenerated?: 'standard' | 'verbose';errorMessage?: string;},dataFormat: DataFormat = 'default',@@ -884,6 +901,7 @@ export class McpResponse implements Response {extensionServiceWorkers?: object[];extensionPages?: object[];comments?: StructuredCommentThread[];+ commentsSnapshotRegenerated?: 'standard' | 'verbose';matchedStyles?: object;errorMessage?: string;navigatedToUrl?: string;@@ -1509,6 +1527,13 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);response.push(compactEncode ? compactEncode(commentsJson) : data.comments.toString(),);+ if (data.commentsSnapshotRegenerated) {+ response.push(+ `Note: DevTools comments regenerated the ${data.commentsSnapshotRegenerated} text snapshot.`,+ );+ structuredContent.commentsSnapshotRegenerated =+ data.commentsSnapshotRegenerated;+ }}if (this.#cssStylesData) {diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.tsindex 32ef695d..13365128 100644--- a/tests/McpResponse.test.ts+++ b/tests/McpResponse.test.ts@@ -14,7 +14,9 @@ import sinon from 'sinon';import type {ParsedArguments} from '../src/config/ConfigParser.js';import type {McpContext} from '../src/McpContext.js';import {McpResponse} from '../src/McpResponse.js';+import {TextSnapshot} from '../src/TextSnapshot.js';import {DevTools, type Extension} from '../src/third_party/index.js';+import type {TextSnapshotNode} from '../src/types.js';import {parseByteSizeRange} from '../src/utils/bytes.js';import {stableIdSymbol} from '../src/utils/id.js';import {@@ -47,6 +49,7 @@ import {createMockHeapSnapshotStats,createMockHeapSnapshotStaticData,createMockMcpContext,+ createMockMcpPage,createMockMcpWorker,createMockObjectInfo,createMockParsedArguments,@@ -1629,4 +1632,162 @@ describe('McpResponse heap snapshot formatting', () => {);});});++ describe('DevTools comments snapshot regeneration note', () => {+ function createSnapshotWithNodes(+ nodes: Array<{id: string; backendNodeId?: number}>,+ verbose = false,+ ): TextSnapshot {+ const idToNode = new Map<string, TextSnapshotNode>();+ const children: TextSnapshotNode[] = [];+ for (const n of nodes) {+ const node: TextSnapshotNode = {+ id: n.id,+ role: 'generic',+ backendNodeId: n.backendNodeId,+ children: [],+ elementHandle: async () => null,+ };+ idToNode.set(n.id, node);+ children.push(node);+ }+ const rootNode: TextSnapshotNode = {+ id: '1_0',+ role: 'root',+ children,+ elementHandle: async () => null,+ };+ return new TextSnapshot({+ root: rootNode,+ idToNode,+ snapshotId: '1',+ hasSelectedElement: false,+ verbose,+ });+ }++ it('includes note when DevTools comments regenerate a standard text snapshot', async () => {+ const response = new McpResponse(createMockParsedArguments());+ const page = createMockMcpPage();+ page.textSnapshot = null;+ const standardSnapshot = createSnapshotWithNodes(+ [{id: '1_1', backendNodeId: 42}],+ false,+ );+ page.resolveBackendNodeId.callsFake(async () => {+ page.textSnapshot = standardSnapshot;+ return '1_1';+ });+ response.setPage(page);+ response.setDevToolsComments([+ {+ id: 'comment-1',+ text: 'Fix the color contrast here',+ node: {backendNodeId: 42, targetId: 'target-1'},+ },+ ]);++ const context = createMockMcpContext();+ const {content, structuredContent} = await response.handle(context);+ const text = getTextContent(content[0]);++ assert.ok(+ text.includes(+ 'Note: DevTools comments regenerated the standard text snapshot.',+ ),+ );+ assert.strictEqual(+ Reflect.get(structuredContent, 'commentsSnapshotRegenerated'),+ 'standard',+ );+ });++ it('includes note when DevTools comments regenerate a verbose text snapshot', async () => {+ const response = new McpResponse(createMockParsedArguments());+ const page = createMockMcpPage();+ page.textSnapshot = null;+ const verboseSnapshot = createSnapshotWithNodes(+ [{id: '1_1', backendNodeId: 42}],+ true,+ );+ page.resolveBackendNodeId.callsFake(async () => {+ page.textSnapshot = verboseSnapshot;+ return '1_1';+ });+ response.setPage(page);+ response.setDevToolsComments([+ {+ id: 'comment-1',+ text: 'Fix the color contrast here',+ node: {backendNodeId: 42, targetId: 'target-1'},+ },+ ]);++ const context = createMockMcpContext();+ const {content, structuredContent} = await response.handle(context);+ const text = getTextContent(content[0]);++ assert.ok(+ text.includes(+ 'Note: DevTools comments regenerated the verbose text snapshot.',+ ),+ );+ assert.strictEqual(+ Reflect.get(structuredContent, 'commentsSnapshotRegenerated'),+ 'verbose',+ );+ });++ it('does not include note when DevTools comments do not regenerate the snapshot', async () => {+ const response = new McpResponse(createMockParsedArguments());+ const page = createMockMcpPage();+ const existingSnapshot = createSnapshotWithNodes(+ [{id: '1_1', backendNodeId: 42}],+ false,+ );+ page.textSnapshot = existingSnapshot;+ page.resolveBackendNodeId.resolves('1_1');+ response.setPage(page);+ response.setDevToolsComments([+ {+ id: 'comment-1',+ text: 'Fix the color contrast here',+ node: {backendNodeId: 42, targetId: 'target-1'},+ },+ ]);++ const context = createMockMcpContext();+ const {content, structuredContent} = await response.handle(context);+ const text = getTextContent(content[0]);++ assert.ok(!text.includes('Note: DevTools comments regenerated'));+ assert.strictEqual(+ Reflect.get(structuredContent, 'commentsSnapshotRegenerated'),+ undefined,+ );+ });++ it('does not include note when comments have no associated nodes', async () => {+ const response = new McpResponse(createMockParsedArguments());+ const page = createMockMcpPage();+ page.textSnapshot = null;+ response.setPage(page);+ response.setDevToolsComments([+ {+ id: 'comment-1',+ text: 'General comment without a node',+ },+ ]);++ const context = createMockMcpContext();+ const {content, structuredContent} = await response.handle(context);+ const text = getTextContent(content[0]);++ assert.ok(!text.includes('Note: DevTools comments regenerated'));+ assert.strictEqual(+ Reflect.get(structuredContent, 'commentsSnapshotRegenerated'),+ undefined,+ );+ });+ });});diff --git a/tests/mocks.ts b/tests/mocks.tsindex b90139d4..3f56d7a6 100644--- a/tests/mocks.ts+++ b/tests/mocks.ts@@ -284,6 +284,7 @@ export function createMockMcpPage(} = {},): MockMcpPage {const page = sinon.createStubInstance(McpPage);+ page.emulationSettings = {};const pptrPage = options.pptrPage ?? createMockPuppeteerPage();const target = options.target ?? createMockPuppeteerTarget({page: pptrPage});page.init.resolves();