-
Notifications
You must be signed in to change notification settings - Fork 475
feat: attach the driving agent to telemetry events #8504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+175
−2
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
98d1e39
feat: add getDrivingAgent agent-detection helper
seancdavis 84c9b48
test: cover getDrivingAgent signals, nesting, and ignored markers
seancdavis 38c708f
docs: document NETLIFY_AGENT for agent detection
seancdavis ba67e09
fix: guard agent-name lookup against prototype keys
seancdavis 608b21e
test: consolidate prototype-key regression cases
seancdavis 5154f78
feat: detect Warp runs and let NETLIFY_AGENT override unconditionally
seancdavis 5b2eae4
fix: address CodeRabbit review on agent detection
seancdavis 4ec9772
fix: tighten agent-detection precedence and input hardening
seancdavis 000b3b8
fix: honor name@version announcements and let the first match win
seancdavis 6a205b5
feat: recognize documented AI_AGENT aliases
seancdavis 4d55c4d
Merge remote-tracking branch 'origin/main' into ex-3040-agent-detection
seancdavis 33d6d51
feat: attach the driving agent to telemetry events
seancdavis 4f10610
fix: keep agent telemetry fields authoritative and document them
seancdavis 70e9ccd
Merge remote-tracking branch 'origin/main' into ex-3042-agent-telemetry
seancdavis 996073d
fix: keep an announced "other" value in otherValue
seancdavis 94d0081
Merge branch 'main' into ex-3042-agent-telemetry
seancdavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,3 +216,27 @@ | |
|
|
||
|
|
||
| <!-- AUTO-GENERATED-CONTENT:END --> | ||
|
|
||
| ## Agent detection | ||
|
|
||
| The CLI reads the `NETLIFY_AGENT` environment variable to learn which AI agent or tool is running it. Agents, MCP servers, and wrappers that invoke the CLI should set it to their name, optionally followed by `@` and a version: | ||
|
|
||
| ```bash | ||
| [email protected] netlify deploy | ||
| ``` | ||
|
|
||
| Recognized values are `claude`, `codex`, `copilot`, `gemini`, `cursor`, `opencode`, `kiro`, `cline`, `amp`, `warp`, `claudeai`, and `chatgpt`, plus the aliases `claude-code`, `claude-ai`, `github-copilot`, `github-copilot-cli`, `github-copilot-vscode-agent`, `cursor-cli`, `gemini-cli`, `kiro-cli`, and `warp-oz`. Matching ignores case and treats `_` as `-`. Any other value is recorded as `other`. Characters other than letters, digits, `_`, `.`, and `-` are removed, and values are truncated to 64 characters. | ||
|
|
||
| The CLI also recognizes markers that agent products set on their own, such as `AI_AGENT`, `CODEX_CI`, and `GEMINI_CLI`. `NETLIFY_AGENT` takes precedence over all of them, even when its value isn't recognized. | ||
|
Check warning on line 230 in docs/index.md
|
||
|
|
||
| ### Telemetry | ||
|
|
||
| When telemetry is enabled, each CLI telemetry event includes the detected agent: | ||
|
|
||
| - `agent`: the recognized name, or `other` | ||
| - `agent_source`: the name of the environment variable that identified the agent | ||
| - `agent_version`: the version, when the agent provides one | ||
| - `agent_markers`: every detected agent name, when markers from more than one agent are present | ||
| - `agent_other_value`: the sanitized value, when `agent` is `other` | ||
|
|
||
| No agent fields are sent when no agent is detected. Telemetry isn't sent in CI, or at all after you run `netlify --telemetry-disable`. | ||
|
Check warning on line 242 in docs/index.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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,6 +271,16 @@ test.each(['constructor', '__proto__', 'toString'])('%s does not resolve via the | |
| }) | ||
| }) | ||
|
|
||
| test.each(['NETLIFY_AGENT', 'AI_AGENT'])('%s=other keeps the announced value in otherValue', (source) => { | ||
| expect(getDrivingAgent({ [source]: 'other' })).toEqual({ name: 'other', source, otherValue: 'other' }) | ||
| expect(getDrivingAgent({ [source]: '[email protected]' })).toEqual({ | ||
| name: 'other', | ||
| source, | ||
| version: '1.0', | ||
| otherValue: 'Other', | ||
| }) | ||
| }) | ||
|
|
||
| test('NETLIFY_AGENT=constructor_1-0_agent does not resolve constructor via the split-at-last-underscore path', () => { | ||
| expect(getDrivingAgent({ NETLIFY_AGENT: 'constructor_1-0_agent' })).toEqual({ | ||
| name: 'other', | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import { track } from '../../../../src/utils/telemetry/telemetry.js' | ||
| import { cliVersion } from '../../../../src/utils/telemetry/utils.js' | ||
| import execa from '../../../../src/utils/execa.js' | ||
|
|
||
| vi.mock('ci-info', () => ({ isCI: false })) | ||
|
|
||
| vi.mock('@netlify/dev-utils', async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import('@netlify/dev-utils')>()), | ||
| getGlobalConfigStore: vi.fn(() => | ||
| Promise.resolve({ get: (key: string) => (key === 'telemetryDisabled' ? false : 'test-user-1') }), | ||
| ), | ||
| })) | ||
|
|
||
| vi.mock('../../../../src/utils/execa.js', () => ({ default: vi.fn(() => ({ unref: vi.fn() })) })) | ||
|
|
||
| const AGENT_ENV_KEYS = [ | ||
| 'NETLIFY_AGENT', | ||
| 'CODEX_CI', | ||
| 'CODEX_VERSION', | ||
| 'GEMINI_CLI', | ||
| 'COPILOT_CLI', | ||
| 'COPILOT_AGENT_SESSION_ID', | ||
| 'OPENCODE', | ||
| 'OPENCODE_TERMINAL', | ||
| 'AGENT_DISPLAY_OUT', | ||
| 'AGENT_CONTEXT_OUT', | ||
| 'OZ_RUN_ID', | ||
| 'WARP_RUN_ID', | ||
| 'AI_AGENT', | ||
| 'COPILOT_AGENT', | ||
| 'CURSOR_AGENT', | ||
| 'CLINE_ACTIVE', | ||
| 'AGENT', | ||
| 'CLAUDE_CODE_CHILD_SESSION', | ||
| ] | ||
|
|
||
| const getTrackedProperties = (): Record<string, unknown> => { | ||
| const { calls } = vi.mocked(execa).mock | ||
| const [, [, optionsJson]] = calls[calls.length - 1] as [string, string[]] | ||
| return (JSON.parse(optionsJson) as { data: { properties: Record<string, unknown> } }).data.properties | ||
| } | ||
|
|
||
| const getTrackedAgentProperties = () => | ||
| Object.fromEntries( | ||
| Object.entries(getTrackedProperties()).filter(([key]) => key === 'agent' || key.startsWith('agent_')), | ||
| ) | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| AGENT_ENV_KEYS.forEach((key) => vi.stubEnv(key, undefined)) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs() | ||
| }) | ||
|
|
||
| describe('track', () => { | ||
| test('adds the driving agent alongside the existing properties', async () => { | ||
| vi.stubEnv('AI_AGENT', 'claude-code') | ||
|
|
||
| await track('command', { command: 'status' }) | ||
|
|
||
| expect(getTrackedProperties()).toMatchObject({ command: 'status', cliVersion }) | ||
| expect(getTrackedAgentProperties()).toEqual({ agent: 'claude', agent_source: 'AI_AGENT' }) | ||
| }) | ||
|
|
||
| test('adds no agent properties when no agent is detected', async () => { | ||
| await track('command', { command: 'status' }) | ||
|
|
||
| expect(getTrackedAgentProperties()).toEqual({}) | ||
| }) | ||
|
|
||
| test('drops agent properties supplied by the caller', async () => { | ||
| await track('command', { | ||
| command: 'status', | ||
| agent: 'spoofed', | ||
| agent_source: 'spoofed', | ||
| agent_version: 'spoofed', | ||
| agent_markers: ['spoofed'], | ||
| agent_other_value: 'spoofed', | ||
| }) | ||
|
|
||
| expect(getTrackedAgentProperties()).toEqual({}) | ||
| }) | ||
|
|
||
| test('adds the agent version when the agent announces one', async () => { | ||
| vi.stubEnv('AI_AGENT', 'claude-code_2-1-263_agent') | ||
|
|
||
| await track('command', { command: 'status' }) | ||
|
|
||
| expect(getTrackedAgentProperties()).toEqual({ | ||
| agent: 'claude', | ||
| agent_source: 'AI_AGENT', | ||
| agent_version: '2.1.263', | ||
| }) | ||
| }) | ||
|
|
||
| test('lists every matched agent when agents are nested', async () => { | ||
| vi.stubEnv('AI_AGENT', 'claude-code') | ||
| vi.stubEnv('CODEX_CI', '1') | ||
|
|
||
| await track('command', { command: 'status' }) | ||
|
|
||
| expect(getTrackedAgentProperties()).toEqual({ | ||
| agent: 'codex', | ||
| agent_source: 'CODEX_CI', | ||
| agent_markers: ['codex', 'claude'], | ||
| }) | ||
| }) | ||
|
|
||
| test('reports an unknown AI_AGENT value as other with its sanitized value', async () => { | ||
| vi.stubEnv('AI_AGENT', 'some-new-tool') | ||
|
|
||
| await track('command', { command: 'status' }) | ||
|
|
||
| expect(getTrackedAgentProperties()).toEqual({ | ||
| agent: 'other', | ||
| agent_source: 'AI_AGENT', | ||
| agent_other_value: 'some-new-tool', | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use smart apostrophes.
Replace the straight apostrophes in
isn'twithisn’t.lint-docsreports both occurrences.Also applies to: 233-233
🧰 Tools
🪛 GitHub Check: lint-docs
[warning] 221-221:
[vale] reported by reviewdog 🐶
[smart-marks.smartApostrophes] Use a smart apostrophe (’) instead of a straight single quote mark in 'isn't'
Raw Output:
{"message": "[smart-marks.smartApostrophes] Use a smart apostrophe (’) instead of a straight single quote mark in 'isn't'", "location": {"path": "docs/index.md", "range": {"start": {"line": 221, "column": 192}}}, "severity": "WARNING"}
🤖 Prompt for AI Agents
Source: Linters/SAST tools