Skip to content

Chrome DevTools for coding agents

fix: keep a still-open selected page instead of falling back to the first page (#2328)

Thomas Bachemc645eee8751e
diff --git a/src/McpContext.ts b/src/McpContext.ts
index 67c31b2a..d01f6445 100644
--- a/src/McpContext.ts
+++ b/src/McpContext.ts
@@ -695,10 +695,13 @@ export class McpContext implements Context {
);
});
+ // Only fall back when the selected page is actually gone. Gating on
+ // `isClosed()` instead of `#pages` membership avoids silently swapping a
+ // live page that is momentarily missing from the snapshot, e.g., a
+ // `devtools://` page, which is selectable but filtered out of `#pages` above.
this.#selectedPageFallback = undefined;
if (
- (!this.#selectedPage ||
- this.#pages.indexOf(this.#selectedPage.pptrPage) === -1) &&
+ (!this.#selectedPage || this.#selectedPage.pptrPage.isClosed()) &&
this.#pages[0]
) {
// Record the automatic change so the response can surface it. Skipped on
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index c24cfb8e..8e5cc869 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -175,12 +175,13 @@ describe('McpContext', () => {
});
});
- it('reports the fallback when the selected page is missing from the list', async () => {
+ it('keeps a still-open selected page that is missing from the list', async () => {
await withMcpContext(async (_response, context) => {
const page = await context.newPage();
assert.ok(context.isPageSelected(page.pptrPage));
- // A live page that is temporarily missing from the pages list.
+ // A live page that is temporarily missing from the pages list must keep
+ // its selection — only a genuinely closed page is replaced.
const pages = await context.browser.pages();
const stub = sinon
.stub(context.browser, 'pages')
@@ -191,9 +192,11 @@ describe('McpContext', () => {
stub.restore();
}
- const fallback = context.getSelectedPageFallback();
- assert.ok(fallback, 'fallback should be reported');
- assert.strictEqual(fallback.wasClosed, false);
+ assert.ok(
+ context.isPageSelected(page.pptrPage),
+ 'a still-open page should keep its selection',
+ );
+ assert.strictEqual(context.getSelectedPageFallback(), undefined);
});
});