[STG-2486] feat(cli): stamp detected agent on session userMetadata#2323
Draft
shrey150 wants to merge 1 commit into
Draft
[STG-2486] feat(cli): stamp detected agent on session userMetadata#2323shrey150 wants to merge 1 commit into
shrey150 wants to merge 1 commit into
Conversation
The CLI already computes the coding agent via detectAgent() but only sends it to PostHog telemetry. Stamp it onto the Browserbase session-create `userMetadata` (`agent` key) alongside the existing browse_cli/cli_version/ install_id attribution, so CLI-created cloud sessions can be attributed to the coding agent (Claude Code / Cursor / Codex / etc.) in Snowflake. - Only set when detectAgent() returns a value; best-effort, never throws. - Sanitized through toMetadataValue() like the other attribution keys. - Adds contract tests for the detected and no-agent cases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 985ea63 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Linear: STG-2486
What
Stamp the detected coding agent onto the Browserbase session
userMetadataat session create, under a newagentkey (e.g.agent: "claude","cursor","codex","hermes").Scope is the
browseCLI package only (packages/cli). The single behavioral change is inapplyCliAttribution()inpackages/cli/src/commands/cloud/sessions/create.ts: after the existingbrowse_cli/cli_version/install_idstamping, it now also callsdetectAgent()and — only when a value is returned — sanitizes it throughtoMetadataValue()and setsuserMetadata.agent.Why
The CLI already computes the coding agent via
detectAgent()(packages/cli/src/lib/agent.ts), but today that value only flows to PostHog telemetry (packages/cli/src/lib/telemetry.ts) — it is not stamped on the Browserbase session itself. Without it on the session, we cannot attribute cloud sessions to the coding agent that drove them (Claude Code / Cursor / Codex / etc.) in our internal analytics.This is the CLI-side prerequisite for that attribution: adding
agentto the session metadata mirrors theagentproperty already sent to PostHog, so our internal analytics can join sessions back to the agent.Design notes:
agentis only set whendetectAgent()returns a truthy value (and the sanitized value is non-empty). No agent → noagentkey, matching howinstall_idis handled.detectAgent()is wrapped in.catch(() => null), so a detection failure can never break session creation.toMetadataValue()(same as the other attribution keys) so the session-create validator never 400s on a stray character or over-length value.userMetadata(via--body/--stdin) is untouched except for the authoritative attribution keys.Note: the sibling driver path (
packages/cli/src/lib/driver/remote.ts, used byopen --remote) stamps the samebrowse_cli/cli_version/install_idkeys but is out of scope for this PR, which targets theapplyCliAttribution(sessions create) path per the task. It can be given the sameagentkey in a follow-up if desired.Changeset
Added a PATCH changeset for the
browsepackage.browseis in the changesetignorelist, but release-impacting CLI changes still get a patch changeset (this changes the session metadata the CLI sends).E2E Test Matrix
pnpm --filter browse run check(tsc --noEmit) afterpnpm turbo run build --filter browse@browserbasehq/stagehand; those errors are pre-existing/unrelated and clear once the workspace dep is built via turbo.)pnpm turbo run build --filter browse4 successful, 4 total;browse:buildexecutedtsc -p tsconfig.json+ oclif manifestcreate.tscompiles intodist/(verifieddist/commands/cloud/sessions/create.jscontainsdetectAgent()+userMetadata.agent = sanitized).vitest run tests/cli-cloud-contract.test.ts tests/agent.test.tsTest Files 2 passed,Tests 57 passedbin/run.js) as a subprocess against a fake Browserbase server and assert the actual POST/v1/sessionsbody.HERMES_SESSION_PLATFORM=telegram)userMetadata.agent === "hermes"userMetadata.browse_cli === "true",userMetadata.agent === undefinedagentkey is omitted when nothing is detected, while base attribution still flows.eslint src/commands/cloud/sessions/create.ts tests/cli-cloud-contract.test.tsNot run locally: no live Browserbase session was created against production (overnight autonomous; nothing irreversible). The contract tests exercise the exact request body via a fake server subprocess, which is the load-bearing behavior here; a real live session would only additionally confirm the Browserbase API accepts the
agentkey (it accepts arbitraryuserMetadatatoday).🤖 Generated with Claude Code
Summary by cubic
Stamp the detected coding agent on session create in the
browseCLI by settinguserMetadata.agent. This enables internal analytics attribution of CLI-created cloud sessions to agents and aligns with PostHog telemetry (Linear STG-2486).userMetadata.agentfromdetectAgent()inapplyCliAttribution(); value sanitized viatoMetadataValue().userMetadata;browse_cli/cli_version/install_id/agentoverride caller values.sessions createpath only; driveropen --remoteunchanged.browse.Written for commit 985ea63. Summary will update on new commits.