-
Notifications
You must be signed in to change notification settings - Fork 698
fix: split Harness name and version in persona analytics (CLI-1771) #7162
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6ed8f34
fix: split Harness name and version in persona analytics (CLI-1771)
nick-y-snyk ce5005c
Merge branch 'main' into fix/CLI-1771-harness-name-version-split
nick-y-snyk 61aa850
Merge branch 'main' into fix/CLI-1771-harness-name-version-split
nick-y-snyk bc1ca18
fix: unblock 3rd-party license bundling for detect-agent
nick-y-snyk ba05a21
Merge branch 'fix/CLI-1771-harness-name-version-split' of github.com:…
nick-y-snyk 33479a0
fix: split Harness version on '@' and isolate agent-detection tests
nick-y-snyk cd38d65
Merge remote-tracking branch 'fork/fix/CLI-1771-harness-name-version-…
nick-y-snyk 4892a2f
Merge branch 'main' into fix/CLI-1771-harness-name-version-split
nick-y-snyk 81a9e18
fix: isolate env in vercel contract tests, fix lint
nick-y-snyk a7c06c9
Merge remote-tracking branch 'origin/fix/CLI-1771-harness-name-versio…
nick-y-snyk a6b6b8e
fix: split single-component '@' versions (e.g. devin@1)
nick-y-snyk 6c0073e
refactor: collapse '_/' and '@' version matching into one regex
nick-y-snyk 4f3592b
fix: restore env vars with os.Setenv, not t.Setenv, in test cleanup
nick-y-snyk 5270885
fix: split '@' versions even for unrecognised harnesses
nick-y-snyk df8c6e9
Merge branch 'main' into fix/CLI-1771-harness-name-version-split
nick-y-snyk 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
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
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 |
|---|---|---|
| @@ -1,133 +1,91 @@ | ||
| // This file is adapted from @vercel/detect-agent (v1.2.3) by Vercel, Inc. | ||
| // Source: https://github.com/vercel/vercel/blob/0d0b990edda112c5cc91e95e0d054878542fe3be/packages/detect-agent/src/index.ts | ||
| // | ||
| // Original work Copyright 2017 Vercel, Inc. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // This Go port has been modified from the original TypeScript source. | ||
|
|
||
| // Package agent identifies the coding-tool Harness driving the current CLI | ||
| // invocation (e.g. Claude Code, Cursor, Codex), split into a canonical name | ||
| // and, when one can be determined, a version. | ||
| package agent | ||
|
|
||
| import ( | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Agent is the canonical name of a known AI coding agent / harness. | ||
| type Agent string | ||
|
|
||
| const ( | ||
| AgentCursor Agent = "cursor" | ||
| AgentCursorCLI Agent = "cursor-cli" | ||
| AgentClaude Agent = "claude" | ||
| AgentCowork Agent = "cowork" | ||
| AgentDevin Agent = "devin" | ||
| AgentReplit Agent = "replit" | ||
| AgentGemini Agent = "gemini" | ||
| AgentCodex Agent = "codex" | ||
| AgentAntigravity Agent = "antigravity" | ||
| AgentAugmentCLI Agent = "augment-cli" | ||
| AgentOpenCode Agent = "opencode" | ||
| AgentGitHubCopilot Agent = "github-copilot" | ||
| AgentV0 Agent = "v0" | ||
| detectagent "github.com/vercel/detect-agent" | ||
| ) | ||
|
|
||
| // devinMarkerPath is a filesystem marker present inside the Devin sandbox. | ||
| const devinMarkerPath = "/opt/.devin" | ||
|
|
||
| // lookup abstracts the environment so detection can be tested without mutating | ||
| // the real process environment. | ||
| type lookup struct { | ||
| getenv func(string) string | ||
| fileExists func(string) bool | ||
| } | ||
|
|
||
| func osLookup() lookup { | ||
| return lookup{ | ||
| getenv: os.Getenv, | ||
| fileExists: func(path string) bool { | ||
| _, err := os.Stat(path) | ||
| return err == nil | ||
| }, | ||
| // DetectAgent resolves the Harness identifier the current process declared | ||
| // via AI_AGENT, or that detect-agent recognised from an environment | ||
| // signature. The value is returned exactly as detect-agent produced it: an | ||
| // explicit AI_AGENT declaration passes through verbatim (trimmed), a | ||
| // signature match is already canonical. No version split or canonicalisation | ||
| // is applied here — see SplitVersion. | ||
| func DetectAgent() (string, bool) { | ||
| details, err := detectagent.Detect() | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| return details.Name, true | ||
| } | ||
|
|
||
| // signature describes how a single agent is recognised from the environment. | ||
| type signature struct { | ||
| agent Agent | ||
| match func(l lookup) bool | ||
| } | ||
| // versionSuffix recognises a version-shaped suffix on a Harness identifier, | ||
| // one of two ways: | ||
| // | ||
| // - fused onto the name with '_' or '/', with an optional trailing | ||
| // role/surface segment (captured only so it isn't absorbed into the | ||
| // version, then discarded). These are ordinary characters that can end a | ||
| // real name on their own, so at least two numeric components are | ||
| // required to avoid matching a name that merely ends in a single digit. | ||
| // - detect-agent's documented name@version convention for custom AI_AGENT | ||
| // declarations (README example: "devin@1"). '@' is never part of a | ||
| // Harness name, so the separator alone is the version signal and a | ||
| // single numeric component is enough. | ||
| // | ||
| // Exactly one of the two version groups is populated per match. | ||
| var versionSuffix = regexp.MustCompile(`^(?P<name>.+?)(?:[_/](?P<version1>\d+(?:[.\-]\d+){1,3})(?:[_/](?P<role>[A-Za-z0-9-]+))?|@(?P<version2>\d+(?:[.\-]\d+){0,3}))$`) | ||
|
|
||
| // anySet reports true if any of the given environment variables is set to a | ||
| // non-empty value. | ||
| func anySet(l lookup, keys ...string) bool { | ||
| for _, k := range keys { | ||
| if l.getenv(k) != "" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| // knownNames maps a normalised (lowercased, separators stripped) form of | ||
| // every Harness name detect-agent knows about to that name's canonical | ||
| // spelling, so a version-stripped fragment resolves to the same vocabulary | ||
| // detect-agent itself uses rather than a hand-written alias list. | ||
| var knownNames = buildKnownNames() | ||
|
|
||
| // signatures is the ordered list of agent detectors. Order matters: the first | ||
| // match wins, so more specific signatures must precede more generic ones. | ||
| var signatures = []signature{ | ||
| {AgentCursor, func(l lookup) bool { return anySet(l, "CURSOR_TRACE_ID") }}, | ||
| {AgentCursorCLI, func(l lookup) bool { | ||
| return anySet(l, "CURSOR_AGENT") || l.getenv("CURSOR_EXTENSION_HOST_ROLE") == "agent-exec" | ||
| }}, | ||
| {AgentGemini, func(l lookup) bool { return anySet(l, "GEMINI_CLI") }}, | ||
| {AgentCodex, func(l lookup) bool { return anySet(l, "CODEX_SANDBOX", "CODEX_CI", "CODEX_THREAD_ID") }}, | ||
| {AgentAntigravity, func(l lookup) bool { return anySet(l, "ANTIGRAVITY_AGENT") }}, | ||
| {AgentAugmentCLI, func(l lookup) bool { return anySet(l, "AUGMENT_AGENT") }}, | ||
| {AgentOpenCode, func(l lookup) bool { return anySet(l, "OPENCODE_CLIENT") }}, | ||
| // Claude Code: the "cowork" surface is a more specific variant and must be | ||
| // checked first. | ||
| {AgentCowork, func(l lookup) bool { | ||
| return anySet(l, "CLAUDECODE", "CLAUDE_CODE") && anySet(l, "CLAUDE_CODE_IS_COWORK") | ||
| }}, | ||
| {AgentClaude, func(l lookup) bool { return anySet(l, "CLAUDECODE", "CLAUDE_CODE") }}, | ||
| {AgentReplit, func(l lookup) bool { return anySet(l, "REPL_ID") }}, | ||
| {AgentGitHubCopilot, func(l lookup) bool { | ||
| return anySet(l, "COPILOT_MODEL", "COPILOT_ALLOW_ALL", "COPILOT_GITHUB_TOKEN") | ||
| }}, | ||
| {AgentDevin, func(l lookup) bool { return l.fileExists(devinMarkerPath) }}, | ||
| func buildKnownNames() map[string]string { | ||
| m := make(map[string]string, len(detectagent.KnownAgents)) | ||
| for _, name := range detectagent.KnownAgents { | ||
| m[normalize(name)] = name | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| // DetectAgent resolves the active AI agent, if any, from the current process | ||
| // environment. | ||
| func DetectAgent() (Agent, bool) { | ||
| return detectAgent(osLookup()) | ||
| func normalize(s string) string { | ||
| return strings.NewReplacer("_", "", "-", "", "/", "").Replace(strings.ToLower(s)) | ||
| } | ||
|
|
||
| // detectAgent resolves the active AI agent, if any. | ||
| // SplitVersion splits a version-shaped suffix off a raw Harness identifier, | ||
| // canonicalising the remaining name fragment against detect-agent's known | ||
| // vocabulary when it resolves to one. It returns the identifier unchanged | ||
| // with an empty version when no version-shaped suffix is present. | ||
| // | ||
| // The AI_AGENT environment variable is the explicit, highest-priority signal: | ||
| // when set it is trusted verbatim (with a couple of canonicalisations). When it | ||
| // is absent, detection falls back to per-agent environment / filesystem | ||
| // signatures. | ||
| func detectAgent(l lookup) (Agent, bool) { | ||
| if name := strings.TrimSpace(l.getenv("AI_AGENT")); name != "" { | ||
| return canonicalAgent(name), true | ||
| // '_'/'/' are ordinary characters that can end a real name on their own, so | ||
| // that split is only trusted when the fragment resolves to a known | ||
| // Harness — never a guess. '@' is never part of a Harness name (detect-agent's | ||
| // own documented convention), so it is unambiguous even for a Harness we | ||
| // don't recognise: the fragment is still split off, just left uncanonicalised. | ||
| func SplitVersion(raw string) (name string, version string) { | ||
| m := versionSuffix.FindStringSubmatch(raw) | ||
| if m == nil { | ||
| return raw, "" | ||
| } | ||
|
|
||
| for _, s := range signatures { | ||
| if s.match(l) { | ||
| return s.agent, true | ||
| fragment := m[versionSuffix.SubexpIndex("name")] | ||
| canonical, known := knownNames[normalize(fragment)] | ||
|
|
||
| if v := m[versionSuffix.SubexpIndex("version2")]; v != "" { | ||
| if known { | ||
| fragment = canonical | ||
| } | ||
| return fragment, strings.ReplaceAll(v, "-", ".") | ||
| } | ||
|
|
||
| return "", false | ||
| } | ||
|
|
||
| // canonicalAgent normalises an explicitly declared AI_AGENT value onto the set | ||
| // of canonical agent names. | ||
| func canonicalAgent(name string) Agent { | ||
| if name == "github-copilot-cli" { | ||
| return AgentGitHubCopilot | ||
| if !known { | ||
| return raw, "" | ||
| } | ||
| return Agent(name) | ||
| return canonical, strings.ReplaceAll(m[versionSuffix.SubexpIndex("version1")], "-", ".") | ||
| } | ||
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.