Skip to content

feat: include source location in list_console_messages - #2060

Closed
masamaru0513 wants to merge 3 commits into
ChromeDevTools:mainfrom
masamaru0513:feat/console-source-location
Closed

masamaru0513 wants to merge 3 commits into
ChromeDevTools:mainfrom
masamaru0513:feat/console-source-location

Conversation

@masamaru0513

@masamaru0513 masamaru0513 commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Include source-mapped location in list_console_messages with shortened display and grouping support.

Fixes #903

Changes since review feedback

Addressed @OrKoN's feedback:

  1. Source-mapped locations — Resolves source-mapped locations (via uiSourceCode.uiLocation()) instead of raw CDP locations
  2. Shortened display — Extracts the filename from URLs to reduce token consumption (e.g., app.js:42:15 instead of https://example.com/static/js/app.js:42:15). Handles puppeteer-style wrappers and percent-encoded URLs in a single non-recursive pass.
  3. Grouping uses location — Messages with same text, type, argCount, AND location are grouped as [N times]
  4. Timeout guard — Bounds stack-trace resolution with a timeout so dialog-blocked pages do not stall the tool call

Output example

msgid=1 [error] Something went wrong (1 args) app.js:42:15
msgid=2 [error] repeated error (1 args) utils.js:10:3 [5 times]

Design

  • list_console_messages: Shows shortened source-mapped location (filename:line:col)
  • get_console_message: Shows full detail with Location: line + ### Stack trace section
  • structuredContent: Includes location object with url, displayName, lineNumber, columnNumber
  • Grouping key: type + text + argCount + location — identical messages from the same source are collapsed

Compatibility

No breaking changes to existing tool outputs or public APIs. The new location field is additive in structuredContent.

Why the timeout wrapper is part of this PR

This PR resolves the stack trace eagerly on every list_console_messages call (not just when fetchDetailedData=true) to derive the source-mapped location. As a side effect, pages paused on a dialog can hang the underlying CDP calls.

The timeout wrapper bounds that work so the tool call always returns. The existing when dialog is open test (#1977) covers this scenario; without the wrapper it times out at 120s.

So the wrapper is not a separate concern — it's required by the eager stack resolution that makes the location feature work.

Test plan

  • Inline script location shows script.js:1:18 (not full pptr URL)
  • External script location shows shortened filename
  • 5 identical messages grouped as [5 times]
  • UncaughtError includes shortened location
  • get_console_message shows ID, Location, Stack trace
  • Dialog-blocked page returns response within timeout
  • All existing console/formatter tests pass

@OrKoN OrKoN left a comment

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.

per #903 we do want to have source maps locations. Additionally, we had non-source mapped location previously and received complains that with many console messages it consumes to many tokens as the URLs can be long and repeating. So we need to be smart about how we display it and take the grouping of console messages into account. In general, agents can make multiple tool calls at once so over the long term calling get_console_message multiple times should be less of a problem. With that, let's make sure use source location for grouping and that we show source mapped location.

@masamaru0513

masamaru0513 commented May 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. To make sure I go in the right direction:

  1. Timing of source map resolution: Should location be resolved lazily when list_console_messages is called, or eagerly in the background as messages are collected?
  2. Token efficiency: For the text output, should I shorten/omit the URL (e.g., show only filename + line:col), or keep location only in structuredContent (JSON) to avoid bloating the text response?

Based on your review, I'll go ahead and:

  1. Resolve source-mapped locations (using the top non-ignored stack frame, similar to the approach in feat(list_console_messages): include source-mapped location of messages #960)
  2. Use source location for grouping and show only filename:line:col in the text output to keep token usage low

Will push an update.

masamaru0513 added a commit to masamaru0513/chrome-devtools-mcp that referenced this pull request May 15, 2026
Address reviewer feedback on PR ChromeDevTools#2060:
- Resolve source-mapped location via the stack trace's top non-ignored
  frame using uiSourceCode.uiLocation(), similar to the approach in ChromeDevTools#960.
- Use the short display name (filename:line:col) for the concise text
  output to keep token usage low; full URL is preserved in the
  structured JSON output.
- Include source location in the grouping key so that messages with
  identical text but different origins do not collapse together.
- Bound stack-trace resolution with a timeout so a hanging page (e.g.
  blocked on a dialog) does not stall the tool call; falls back to the
  raw msg.location() / exceptionDetails location.
- Split SymbolizedError.fromDetails' `includeStackAndCause` into
  `includeStack` and `includeCause` so the formatter can fetch the
  stack for location resolution without paying for cause lookup.
@masamaru0513
masamaru0513 force-pushed the feat/console-source-location branch from 4ffbc86 to 2e214ea Compare May 15, 2026 14:34
@masamaru0513
masamaru0513 requested a review from OrKoN May 15, 2026 14:57
@masamaru0513

Copy link
Copy Markdown
Contributor Author

@OrKoN one self-review note before you look — I added a few things that may be out of scope for this PR:

  1. A short withResolveTimeout wrapper around stack resolution to guard against pages paused on a dialog. Happy to split this into a separate PR if you prefer.
  2. Splitting includeStackAndCause into includeStack / includeCause so the formatter can fetch the stack without paying for cause lookup. Also fine to keep the combined flag and resolve both, if you'd rather not change the API here.
  3. Also unsure whether shortUrl() is worth carrying — uiSourceCode.displayName() covers the common case; the regex parsing is mainly to extract the inner file from pptr:evaluate;... URLs. Open to dropping it.

Let me know which of these you want trimmed and I'll push a focused update.

- Add location (filename:line:col) to list_console_messages output
- Source-mapped location with shortened display (shortUrl)
- Handle pptr: protocol and encoded URLs
- Remove deprecated includeStackAndCause option
@masamaru0513
masamaru0513 force-pushed the feat/console-source-location branch from 2e214ea to 84d6f62 Compare May 20, 2026 14:16
@masamaru0513

masamaru0513 commented May 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@OrKoN Addressed your feedback. Reverted the includeStackAndCause API change and simplified shortUrl() to ~15 lines (no more recursive unwrapping or URL parsing). The timeout wrapper stays as it's needed to prevent hangs when the page is paused on a dialog. Ready for review.

Would appreciate another look when you get a chance.

@masamaru0513

Copy link
Copy Markdown
Contributor Author

@OrKoN Could you approve the workflows so CI can run? Happy to address anything that comes up.

@masamaru0513

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN, sorry to bother you again. The CI failure on the previous run was in an unrelated test (work for elements inside iframes in script.test.js — a navigation timeout on Windows/Node 26), not related to my changes. Could you re-run the workflow when you get a chance? Thank you!

@OrKoN

OrKoN commented May 26, 2026

Copy link
Copy Markdown
Collaborator

@masamaru0513 please do not rebase the PR for now, as it will reset the test results.

@OrKoN
OrKoN requested a review from szuend May 29, 2026 10:54
@OrKoN

OrKoN commented May 29, 2026

Copy link
Copy Markdown
Collaborator

@szuend @Lightning00Blade could you please review and see if it makes sense to land this?

@OrKoN
OrKoN requested review from Lightning00Blade and OrKoN and removed request for OrKoN May 29, 2026 10:55
@masamaru0513

Copy link
Copy Markdown
Contributor Author

@Lightning00Blade Gentle ping — OrKoN requested your review here on May 29. Would you have a chance to take a look when you get a moment? Happy to address any feedback. Thanks!

@szuend

szuend commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

This PR is on my ToDo list for review. Unfortunately it's very large, with non-trivial changes. This will take some time to properly review.

@masamaru0513

Copy link
Copy Markdown
Contributor Author

Thanks for the heads up — happy to wait!

OrKoN pushed a commit to LuHuangMSFT/chrome-devtools-mcp that referenced this pull request Aug 10, 2026
…ols#2505)

## What

Adds an opt-in `includeStackTraces` parameter to
`list_console_messages`. When set, each listed message includes its
resolved (source-mapped, ignore-list-filtered) stack trace — in the text
output as indented frames under the message line, and in
`structuredContent` as a pre-formatted `stackTrace` string, matching the
format `get_console_message` already uses.

## Why

Fixes the workflow described in ChromeDevTools#383: today the list output only
contains plain message text, so an LLM debugging a page has to call
`get_console_message` once per message to learn which file/function/line
failed. With `includeStackTraces: true` the model can map every error to
its source in a single call.

Design notes:

- **Opt-in, default off** — the default output is byte-for-byte
unchanged, keeping the token cost of the common path identical.
- Reuses the existing DevTools stack resolution
(`createStackTraceForConsoleMessage` / `SymbolizedError`) and the
existing `formatStackTrace` formatting, so frames are source-mapped and
ignore-listed exactly like in `get_console_message`.
- The per-message "1-based indexing" note is emitted once for the whole
list instead of once per message to keep the output compact.
- Works for console API messages, uncaught errors (including `Caused
by:` chains), and grouped repeated messages (`[N times]`).

## Before / after

Default (unchanged):

```
## Console messages
Showing 1-1 of 1 (Page 1 of 1).
msgid=1 [log] Hello stack trace! (0 args)
```

With `includeStackTraces: true`:

```
## Console messages
Showing 1-1 of 1 (Page 1 of 1).
msgid=1 [log] Hello stack trace! (0 args)
  at foo (foo.ts:10:2)
  at bar (foo.ts:20:2)
  --- setTimeout -------------------------
  at schedule (util.ts:5:2)
Note: stack trace line and column numbers use 1-based indexing
```

`structuredContent.consoleMessages[i]` gains an optional `"stackTrace":
"at foo (foo.ts:10:2)\nat bar (foo.ts:20:2)\n..."` field.

## Relationship to ChromeDevTools#2060

Complements ChromeDevTools#2060 (source location for `list_console_messages`, issue
ChromeDevTools#903): that PR surfaces the top-frame *location* for every message by
default, while this one exposes the *full stack trace* behind an
explicit opt-in. The features are independent and the changes are
designed to coexist with minimal conflict.

## Testing

- `npm run build` — passes
- `npm run test:no-build` — full suite passes (exit 0)
- `npm run check-format` — passes
- `npm run gen` — regenerated `docs/tool-reference.md`, CLI options, and
tool-call metrics (included)

New coverage:

- `tests/formatters/ConsoleFormatter.test.ts` — snapshot tests for
concise `toString`/`toJSON` with `fetchStackTrace` set (console message
+ uncaught error) and for the unchanged default.
- `tests/tools/console.test.ts` — integration tests asserting frames
(`at failingFn`) appear in text and `structuredContent` when
`includeStackTraces: true`, and are absent by default.

Fixes ChromeDevTools#383

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: ZayanKhan-12 <[email protected]>
@Lightning00Blade
Lightning00Blade removed their request for review August 10, 2026 11:10
@masamaru0513

Copy link
Copy Markdown
Contributor Author

Now that #2505 has landed, much of what this PR was aiming at is already covered. I'm happy to close this if you consider #903 sufficiently addressed by it.

If it's still wanted, I have a reworked version ready locally that resolves the conflict and builds on the includeStackTraces plumbing from #2505 rather than duplicating it. I haven't pushed it, since that would reset the workflow approval state here — just let me know which you'd prefer.

@OrKoN

OrKoN commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

I think the other PR sufficiently address the use case of #903 Let's close this PR and sorry that we could not land it timely!

@masamaru0513

masamaru0513 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Sure. Thanks for taking a look at this! I will send another PR if I find something else to contribute.

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.

The list_console_messages tool should include the source location where a message originated from

3 participants