Tag events/v2 telemetry with X-Mapbox-Agent during agent-driven test runs#13706
Draft
ctufts wants to merge 1 commit into
Draft
Tag events/v2 telemetry with X-Mapbox-Agent during agent-driven test runs#13706ctufts wants to merge 1 commit into
ctufts wants to merge 1 commit into
Conversation
…driven test runs Browsers forbid script-set User-Agent headers, so there's no way for GL JS itself to signal that a page is being driven by an AI coding agent (Claude Code, Codex, Cursor, etc.). This adds test-only tooling that detects the agent (if any) from the Node test-driver process's environment and forwards it into the browser as `window.__mapboxAgent` before any page script runs, via a Vite `transformIndexHtml` hook wired into the render/query integration test configs. `TelemetryEvent.postEvent` (src/util/mapbox.ts) reads that flag defensively and, when present, adds an `X-Mapbox-Agent` header to the events/v2 request, so agent-driven test/CI traffic against this repo can be identified server-side. This is test tooling only: test/integration/lib/agent-forwarding.ts is never imported by production code and is not part of the shipped bundle. The `postEvent` change is a no-op everywhere `window.__mapboxAgent` isn't set, including a plain `npm run dev` and worker contexts where `window` is absent entirely. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Hey, @ctufts 👋 Thanks for your contribution to Mapbox GL JS! Important: This repository does not accept direct merges. All changes go through our internal review process. What happens next:
Please respond to any review comments on this PR. For more details, see CONTRIBUTING.md. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds test-only tooling that lets this repo's own test suite tag the telemetry
events/v2request it fires when running under an AI coding agent (Claude Code, Codex, Cursor, etc.), so agent-driven test/CI traffic can be told apart from normal usage.Browsers won't let scripts set the
User-Agentheader, and GL JS running in a page has no way to know it's being driven by a coding agent: that signal only exists in the Node process running the Playwright/Vitest test driver, via env vars the agent sets (e.g.CLAUDECODEfor Claude Code). To get that signal from Node into the browser:test/integration/lib/agent-forwarding.ts(new, test-only) checksprocess.envagainst an allowlist of about 20 known agent env-var indicators, first match wins by table order, with a validated fallback to a genericAI_AGENT/AGENTenv var. It never throws. If nothing matches, it returns nothing rather than a vague"unknown", so no header gets sent.transformIndexHtmlplugin, wired into the render and query integration test configs, forwards that id into the browser aswindow.__mapboxAgentbefore any other page script runs (same idea as a Playwrightpage.addInitScript).TelemetryEvent.postEvent(src/util/mapbox.ts) readswindow.__mapboxAgentdefensively and, if it's present, adds anX-Mapbox-Agentheader to the events/v2 request.agent-forwarding.tsis never imported by production code and isn't part of the shipped bundle. ThepostEventchange is a no-op anywherewindow.__mapboxAgentisn't set (npm run dev, a consuming app, or a worker context withoutwindow), so behavior there is unchanged.Testing
postEvent(header added whenwindow.__mapboxAgentis set, absent otherwise).npm run lint,npm run test-typings, andnpm run test-unitpass locally.distbundle and a real browser session), so an actual end-to-end capture of the header on anevents/v2request is unverified beyond the unit tests above.Downstream impact
None for consuming applications: this only touches this repo's own test tooling and adds one conditional header to a telemetry request that's already unauthenticated and opt-in via access token configuration.
Operational considerations
The
X-Mapbox-Agentheader won't be visible/actionable server-side until the events ingestion endpoint is updated to accept and record it (tracked separately, outside this repo).Launch Checklist
@mapbox/map-design-team@mapbox/static-apisif this PR includes style spec API or visual changes.@mapbox/gl-nativeif this PR includes shader changes or needs a native port.@mapbox/gl-nativeif this PR disables any test because it also needs to be disabled on their side.gl-nativeto groom in the MAPSNAT JIRA queue if this PR includes shader changes or features not present in the native side or if it disables a test that's not disabled there.