Add agent detection for Cline, Codex, Antigravity and OpenCode#4496
Add agent detection for Cline, Codex, Antigravity and OpenCode#4496simonfaltum merged 4 commits intomainfrom
Conversation
Refactor agent detection to use a table-driven approach so adding new agents requires only a constant and a table entry. New agents: Cline (CLINE_ACTIVE), Codex (CODEX_CI), OpenCode (OPENCODE). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| ) | ||
| // knownAgents maps environment variables to product names. | ||
| // Adding a new agent only requires a new entry here and a new constant above. | ||
| var knownAgents = []struct { |
There was a problem hiding this comment.
wanted to have copilot as well but it doesn't look like it sets any ENV vars
|
|
||
| ctx = withAgentInUserAgent(ctx) | ||
| assert.Contains(t, useragent.FromContext(ctx), "agent/cursor") | ||
| func TestAgentInUserAgent(t *testing.T) { |
There was a problem hiding this comment.
thought it was best to not repeat the same test 6 times..
|
Commit: a77e8bf
34 interesting tests: 16 flaky, 7 KNOWN, 5 SKIP, 5 RECOVERED, 1 FAIL
Top 50 slowest tests (at least 2 minutes):
|
libs/agent/agent.go
Outdated
There was a problem hiding this comment.
[no action required] The key should ideally be an empty struct.
type productKeyType struct{}
var productKey productKeyTypeThis is the idiomatic way to represent context key: it has zero size and no meaning.
| // Mock is a helper for tests to mock the detection result. | ||
| func Mock(ctx context.Context, product string) context.Context { | ||
| return context.WithValue(ctx, productKey, product) | ||
| } |
There was a problem hiding this comment.
[no action required] Testing utilities are typically exposed is a separate package than the one they test. For example, http testing utilities are in httptest (same for io).
I understand this cannot be done here because the key is private, though, this is potentially a consequence of the package having a blurry responsibility boundary with the package that manages user-agents. For example, we could focus this package purely on agent extraction and let the package responsible for managing the useragent decide how to leverage it. This would allow us to centralize the useragent management in one place, while still having all the agent resolution logic nicely encapsulated. In that context, this function would not need to exist I believe.
pietern
left a comment
There was a problem hiding this comment.
@simonfaltum Can you include references to where you found these env vars?
Just so we can trace them to the source if we need to later.
|
@pietern Added antigravity as well, and updated the sources. A few of them are closed source and it's basically "I tried this locally" :/ |
…text key - Add ANTIGRAVITY_AGENT env var for Google Antigravity detection - Add source reference comments for all agent env vars - Use zero-size struct for context key (idiomatic Go pattern) - Clear all agent env vars in acceptance test config
|
Thanks, the refs are great. When I looked at this last month Codex didn't export this yet. Via your ref I found this PR: openai/codex#9366 |
|
Can you update the PR title as well (or just say "expand"). |
|
It would be great to have Windsurf but I think that will have to come another time as it is closed source |
|
Commit: 884d6b7
33 interesting tests: 17 flaky, 8 KNOWN, 5 SKIP, 3 RECOVERED
Top 50 slowest tests (at least 2 minutes):
|
Summary
CLINE_ACTIVE), Codex (CODEX_CI), OpenCode (OPENCODE) and AntigravityknownAgentsslice, so adding a new agent only requires a new constant and one table entryTest plan
go test ./libs/agent/ -v— all detection tests passgo test ./cmd/root/ -run TestAgent -v— all user agent tests pass🤖 Generated with Claude Code