Skip to content

feat(script): evaluate scripts in dedicated worker contexts - #2352

Closed
gunjanjaswal wants to merge 7 commits into
ChromeDevTools:mainfrom
gunjanjaswal:feat/316-dedicated-worker-execution-context
Closed

gunjanjaswal wants to merge 7 commits into
ChromeDevTools:mainfrom
gunjanjaswal:feat/316-dedicated-worker-execution-context

Conversation

@gunjanjaswal

Copy link
Copy Markdown
Contributor

Adds opt-in support for evaluating JavaScript inside a page's dedicated Web Workers, so agents can reach off-main-thread application logic (e.g. Neo.mjs).

Everything is gated behind a new --experimentalWorkers flag and off by default. Worker contexts are never enumerated unless explicitly requested, so baseline token usage is unchanged.

  • list_dedicated_workers lists the selected page's dedicated workers and assigns each a stable worker id.
  • evaluate_script gains an optional workerId argument that resolves the target through the existing Page | Frame | WebWorker union, mirroring the existing serviceWorkerId path (same mutual-exclusivity and no-element-args guards).

Shared workers (#317) are intentionally left out of scope.

Tests cover both tools and the new evaluate path. build, typecheck, eslint, prettier, and npm run gen (docs + CLI reference) all run clean.

Refs #316

Add opt-in support for enumerating and evaluating JavaScript inside a
page's dedicated Web Workers, so agents can inspect off-main-thread app
logic (e.g. Neo.mjs).

The feature is gated behind a new --experimentalWorkers flag and is off
by default, so worker execution contexts are never enumerated unless
explicitly requested and the baseline token usage is unchanged.

- Add list_dedicated_workers tool to list a page's dedicated workers and
  assign each a stable worker id.
- Add an optional workerId argument to evaluate_script that resolves the
  target through the existing Page | Frame | WebWorker union, mirroring
  the existing serviceWorkerId path.

Refs ChromeDevTools#316
…worker-execution-context

# Conflicts:
#	src/McpContext.ts
…worker-execution-context

# Conflicts:
#	src/telemetry/tool_call_metrics.json
…worker-execution-context

# Conflicts:
#	src/McpContext.ts
…worker-execution-context

# Conflicts:
#	src/telemetry/flag_usage_metrics.json
#	tests/tools/script.test.ts
@gunjanjaswal
gunjanjaswal force-pushed the feat/316-dedicated-worker-execution-context branch from 1f23d38 to bb57f9f Compare August 13, 2026 17:36
@OrKoN

OrKoN commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR! I think it looks good but I will try to do a review next week.

@OrKoN
OrKoN self-requested a review August 21, 2026 19:03
…worker-execution-context

# Conflicts:
#	src/telemetry/tool_call_metrics.json
@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Thanks @OrKoN! I've just rebased it on main to clear the conflict (the tool-metrics file needed my list_dedicated_workers entry re-appended after the new heap-snapshot tool), so it's mergeable again and ready whenever you get to it. No rush.

* Spawns a dedicated worker in the page and resolves once Puppeteer has
* attached to it.
*/
async function spawnDedicatedWorker(page: Page): Promise<void> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move it to test utils?

Comment thread src/tools/script.ts
});

export const listDedicatedWorkers = defineTool({
name: 'list_dedicated_workers',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been thinking if we need a dedicated tool to list workers or if we can just list the workers in the pages response similar to service workers are handled includeExtensionServiceWorkers? I think ideally we could land #2434 first to streamline the interactions with workers.

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Thanks @OrKoN. On the test helper, yes, I'll move it to test utils.

On the bigger question: listing workers in the pages response the way includeExtensionServiceWorkers does makes sense to me. It keeps the worker surface consistent with how service workers already appear rather than adding a separate listing path. And I agree #2434 (extracting worker logic into McpWorker) landing first gives this something clean to build on instead of threading worker handling through script.ts.

Happy to sequence it that way. Would you prefer I pick up #2434 first and then rework this on top of McpWorker (folding the worker listing into the pages response), or is it already spoken for? Either way I'll hold the rebase here until the shape is settled, so I'm not churning the branch against a design that's about to change.

@OrKoN

OrKoN commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

hi @gunjanjaswal, thanks, if you are still interested, let's work on #2434 first. I will close this PR for now to reduce the amount of open PRs but let's re-open it after refactoring is done.

@OrKoN OrKoN closed this Sep 22, 2026
eacp1991 pushed a commit to eacp1991/chrome-devtools-mcp that referenced this pull request Oct 1, 2026
Closes ChromeDevTools#2434.

Worker handling reached through Puppeteer targets directly: the context
stored plain `ExtensionServiceWorker` objects, kept their ids in a side
`WeakMap`, and callers such as the script evaluator resolved
`target.worker()` themselves. This introduces an `McpWorker` class that
mirrors `McpPage`, wrapping a worker target together with its stable id,
`type`, `url`, and a lazy `worker()` accessor, so dedicated and service
workers can share a single representation.

### What changed

- New `src/McpWorker.ts` with an `McpWorker` class and a `WorkerType`
(`service_worker` | `dedicated_worker` | `shared_worker`), plus a small
per-type id prefix so ids never collide across types.
- `McpContext` snapshots into `McpWorker` instances via `getWorkers()` /
`createWorkersSnapshot()`. The id now lives on the worker, so the
separate `getExtensionServiceWorkerId()` lookup is gone.
- `ServiceWorkerConsoleCollector`, the `browser_evaluate_script` worker
resolver, and the extension-service-worker response section consume the
abstraction. The response filters by `type === 'service_worker'`, so the
"Extension Service Workers" listing stays correct once other worker
types start appearing.
- `ExtensionServiceWorker` removed from `types.ts`.

### Scope

No behavior change: the snapshot still tracks only extension service
workers, and the `sw-N` id scheme is preserved (existing tests exercise
the same ids). This is deliberately just the abstraction — surfacing
dedicated workers is left to ChromeDevTools#2352, which should rebase cleanly on top.

`tsc`, eslint, and prettier are clean, and the build passes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants