-
Notifications
You must be signed in to change notification settings - Fork 95
RTECO-1017 - Detect agent + CI invocation context, enrich metrics wit… #1555
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
+215
−17
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aaa4fda
RTECO-1017 - Detect agent + CI invocation context, enrich metrics wit…
fluxxBot 89ba093
fixed comments
fluxxBot 60e0bf5
Enhance execution context handling in tests and implementation. Added…
fluxxBot 6f29b6a
Merge branch 'master' into RTECO-1017-CIdetect
fluxxBot 036d919
Remove EnrichUserAgent function and related tests to streamline execu…
fluxxBot 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "golang.org/x/term" | ||
| ) | ||
|
|
||
| // ExecutionContext describes how a CLI invocation was launched. | ||
| // Dimensions are independent: an agent may run inside CI; both will be reported. | ||
| type ExecutionContext struct { | ||
| Agent string // e.g. "claude", "cursor", "gemini", "" if none | ||
| CISystem string // e.g. "github_actions", "" if not CI | ||
| IsCI bool | ||
| IsAgent bool | ||
| IsInteractive bool // stdin is a TTY | ||
| TraceID string // propagated trace ID (e.g. CURSOR_TRACEID), empty if none | ||
| } | ||
|
|
||
| // agent env var detection table. First match wins. | ||
| var agentEnvDetectors = []struct { | ||
| name string | ||
| envs []string | ||
| }{ | ||
| {"claude", []string{"CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT"}}, | ||
| {"gemini", []string{"GEMINI_CLI"}}, | ||
| {"goose", []string{"GOOSE_TERMINAL"}}, | ||
| {"cursor", []string{"CURSOR_AGENT", "CURSOR_TRACEID"}}, | ||
| {"copilot", []string{"COPILOT_CLI"}}, | ||
| {"kilocode", []string{"KILO_IPC_SOCKET_PATH", "KILO_SERVER_PASSWORD"}}, | ||
|
fluxxBot marked this conversation as resolved.
|
||
| {"roo_code", []string{"ROO_CODE_IPC_SOCKET_PATH"}}, | ||
| {"replit", []string{"REPLIT_AGENT"}}, | ||
| {"windsurf", []string{"WINDSURF_SESSION_ID"}}, | ||
|
fluxxBot marked this conversation as resolved.
Outdated
|
||
| {"aider", []string{"AIDER_MODEL"}}, | ||
| {"codex", []string{"CODEX_HOME"}}, | ||
|
fluxxBot marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // DetectExecutionContext captures all signals about who executed the CLI. | ||
| func DetectExecutionContext() ExecutionContext { | ||
| ec := ExecutionContext{ | ||
| IsInteractive: term.IsTerminal(int(os.Stdin.Fd())), | ||
|
fluxxBot marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| ec.Agent = detectAgent() | ||
| ec.IsAgent = ec.Agent != "" | ||
| ec.TraceID = detectAgentTraceID(ec.Agent) | ||
|
|
||
| ec.CISystem = detectCISystem() | ||
| ec.IsCI = ec.CISystem != "" | ||
|
|
||
| return ec | ||
| } | ||
|
|
||
| func detectAgent() string { | ||
| for _, d := range agentEnvDetectors { | ||
| for _, e := range d.envs { | ||
| if os.Getenv(e) != "" { | ||
| return d.name | ||
| } | ||
| } | ||
| } | ||
| // Fallback: generic AGENT env var (goose convention, codex pending). | ||
| if v := os.Getenv("AGENT"); v != "" { | ||
| return v | ||
| } | ||
|
fluxxBot marked this conversation as resolved.
Outdated
|
||
| return "" | ||
| } | ||
|
|
||
| // detectAgentTraceID returns a trace ID propagated by the parent agent, if any. | ||
| // Only used when the detected agent itself owns the trace ID env var, so we don't | ||
| // reuse a stale ID leaked from an outer shell (e.g. CURSOR_TRACEID present while | ||
| // the actual invoker is Claude Code). Empty result means the CLI should generate | ||
| // its own trace ID. | ||
| func detectAgentTraceID(agent string) string { | ||
| switch agent { | ||
|
Check failure on line 75 in common/commands/execution_context.go
|
||
| case "cursor": | ||
| return os.Getenv("CURSOR_TRACEID") | ||
|
fluxxBot marked this conversation as resolved.
Outdated
|
||
| } | ||
| return "" | ||
| } | ||
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,112 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestDetectAgent_EnvVar(t *testing.T) { | ||
| cases := []struct { | ||
| envVar string | ||
| want string | ||
| }{ | ||
| {"CLAUDECODE", "claude"}, | ||
| {"CLAUDE_CODE_ENTRYPOINT", "claude"}, | ||
| {"GEMINI_CLI", "gemini"}, | ||
| {"GOOSE_TERMINAL", "goose"}, | ||
| {"CURSOR_AGENT", "cursor"}, | ||
| {"CURSOR_TRACEID", "cursor"}, | ||
| {"COPILOT_CLI", "copilot"}, | ||
|
fluxxBot marked this conversation as resolved.
Outdated
|
||
| {"KILO_IPC_SOCKET_PATH", "kilocode"}, | ||
| {"ROO_CODE_IPC_SOCKET_PATH", "roo_code"}, | ||
| {"REPLIT_AGENT", "replit"}, | ||
| {"WINDSURF_SESSION_ID", "windsurf"}, | ||
| {"AIDER_MODEL", "aider"}, | ||
| {"CODEX_HOME", "codex"}, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.envVar, func(t *testing.T) { | ||
| clearAgentEnvVars(t) | ||
| t.Setenv(c.envVar, "1") | ||
| assert.Equal(t, c.want, detectAgent()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDetectAgent_GenericFallback(t *testing.T) { | ||
| clearAgentEnvVars(t) | ||
| t.Setenv("AGENT", "custom_bot") | ||
| assert.Equal(t, "custom_bot", detectAgent()) | ||
| } | ||
|
|
||
| func TestDetectAgent_None(t *testing.T) { | ||
| clearAgentEnvVars(t) | ||
| assert.Equal(t, "", detectAgent()) | ||
| } | ||
|
|
||
| func TestDetectAgentTraceID(t *testing.T) { | ||
| t.Setenv("CURSOR_TRACEID", "trace-abc") | ||
| assert.Equal(t, "trace-abc", detectAgentTraceID("cursor")) | ||
| // Trace ID gated on agent identity: a leaked CURSOR_TRACEID from an outer | ||
| // shell must not be reused when the real invoker is a different agent. | ||
| assert.Equal(t, "", detectAgentTraceID("claude")) | ||
| assert.Equal(t, "", detectAgentTraceID("")) | ||
| } | ||
|
|
||
| func TestDetectExecutionContext_AgentAndCI(t *testing.T) { | ||
| clearAgentEnvVars(t) | ||
| clearCIEnvVars(t) | ||
| t.Setenv("CLAUDECODE", "1") | ||
| t.Setenv("GITHUB_ACTIONS", "true") | ||
|
|
||
| inv := DetectExecutionContext() | ||
| assert.True(t, inv.IsAgent) | ||
| assert.True(t, inv.IsCI) | ||
| assert.Equal(t, "claude", inv.Agent) | ||
| assert.Equal(t, "github_actions", inv.CISystem) | ||
| } | ||
|
|
||
| func TestDetectExecutionContext_CIOnly(t *testing.T) { | ||
| clearAgentEnvVars(t) | ||
| clearCIEnvVars(t) | ||
| t.Setenv("GITHUB_ACTIONS", "true") | ||
|
|
||
| inv := DetectExecutionContext() | ||
| assert.False(t, inv.IsAgent) | ||
| assert.True(t, inv.IsCI) | ||
| assert.Equal(t, "github_actions", inv.CISystem) | ||
| } | ||
|
|
||
| func TestDetectExecutionContext_NoEnv(t *testing.T) { | ||
| clearAgentEnvVars(t) | ||
| clearCIEnvVars(t) | ||
|
|
||
| inv := DetectExecutionContext() | ||
| assert.False(t, inv.IsAgent) | ||
| assert.False(t, inv.IsCI) | ||
| assert.Equal(t, "", inv.Agent) | ||
| assert.Equal(t, "", inv.CISystem) | ||
| } | ||
|
|
||
| func clearAgentEnvVars(t *testing.T) { | ||
| t.Helper() | ||
| for _, d := range agentEnvDetectors { | ||
| for _, e := range d.envs { | ||
| t.Setenv(e, "") | ||
| } | ||
| } | ||
| t.Setenv("AGENT", "") | ||
| } | ||
|
|
||
| func clearCIEnvVars(t *testing.T) { | ||
| t.Helper() | ||
| for _, e := range []string{ | ||
| "JENKINS_URL", "TRAVIS", "CIRCLECI", "GITHUB_ACTIONS", "GITLAB_CI", | ||
| "BUILDKITE", "BAMBOO_BUILD_KEY", "TF_BUILD", "TEAMCITY_VERSION", | ||
| "DRONE", "BITBUCKET_BUILD_NUMBER", "CODEBUILD_BUILD_ID", | ||
| "CI", "CONTINUOUS_INTEGRATION", "BUILD_ID", "BUILD_NUMBER", | ||
| } { | ||
| t.Setenv(e, "") | ||
| } | ||
| } | ||
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
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.