-
Notifications
You must be signed in to change notification settings - Fork 6
Add ai detection to include the agent or model using the cli Closes #459 #462
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
Open
SeanAlexanderHarris
wants to merge
3
commits into
main
Choose a base branch
from
459-add-agent-use-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2918e10
feat(459): :sparkles: add ai detection to include the agent or model …
SeanAlexanderHarris 24ababd
fix(459): reject whitespace in agentenv header value validator
SeanAlexanderHarris 701deed
test(459): fix env-var isolation list in TestExecuteSetsAgentInUserAgent
SeanAlexanderHarris 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,54 @@ | ||
| // Package agentenv detects well-known environment variables set by AI | ||
| // coding agents (Claude Code, Antigravity, etc.) and resolves the driving | ||
| // agent's name so the CLI can advertise it in its User-Agent header, letting | ||
| // the Prolific API attribute requests to the agent that drove them. | ||
| package agentenv | ||
|
|
||
| import "os" | ||
|
|
||
| type agentSource struct { | ||
| env string // env var variable to check | ||
| name string // the agent / model slug, empty is treated as 'forward value' | ||
| } | ||
|
|
||
| var sources = []agentSource{ | ||
| {"CLAUDE_CODE", "claude-code"}, | ||
| {"ANTIGRAVITY_AGENT", "antigravity"}, | ||
| {"AI_AGENT", ""}, | ||
| {"LLM_AGENT", ""}, | ||
| } | ||
|
|
||
| // Detected returns the name of the AI agent driving the CLI, or "" if none is | ||
| // detected. Branded tools map to a fixed slug; generic vars forward their | ||
| // value verbatim, provided it contains no control characters or whitespace. | ||
| // Unset, empty, or malformed values yield "". | ||
| func Detected() string { | ||
| for _, s := range sources { | ||
| val := os.Getenv(s.env) | ||
| if val == "" { | ||
| continue | ||
| } | ||
| name := s.name | ||
| if name == "" { // generic var: forward its value | ||
| name = val | ||
| } | ||
| if !validHeaderValue(name) { | ||
| continue | ||
| } | ||
| return name // first usable match wins | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // validHeaderValue reports whether s is safe to embed as a single | ||
| // space-separated User-Agent token: no control characters, and no | ||
| // whitespace (which would split the token across multiple segments). | ||
| func validHeaderValue(s string) bool { | ||
| for i := 0; i < len(s); i++ { | ||
| b := s[i] | ||
| if b < 0x20 || b == 0x7f || b == ' ' { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
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,102 @@ | ||
| package agentenv | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestDetected(t *testing.T) { | ||
| // Every env var Detected looks at. Each subtest zeroes all of them first | ||
| // so it's isolated from whatever environment its running in (e.g. running | ||
|
SeanAlexanderHarris marked this conversation as resolved.
|
||
| // inside an AI agent, or a local dev machine) has set. | ||
| knownVars := []string{"CLAUDE_CODE", "ANTIGRAVITY_AGENT", "AI_AGENT", "LLM_AGENT"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| env map[string]string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "no agent detected", | ||
| env: map[string]string{}, | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "claude code, branded slug regardless of value", | ||
| env: map[string]string{"CLAUDE_CODE": "1"}, | ||
| want: "claude-code", | ||
| }, | ||
| { | ||
| name: "antigravity, branded slug regardless of value", | ||
| env: map[string]string{"ANTIGRAVITY_AGENT": "ignored-value"}, | ||
| want: "antigravity", | ||
| }, | ||
| { | ||
| name: "generic AI_AGENT forwards its value", | ||
| env: map[string]string{"AI_AGENT": "cursor"}, | ||
| want: "cursor", | ||
| }, | ||
| { | ||
| // tricky scenario, decision for branded | ||
| name: "branded wins over generic (precedence)", | ||
| env: map[string]string{"CLAUDE_CODE": "1", "AI_AGENT": "cursor"}, | ||
| want: "claude-code", | ||
| }, | ||
| { | ||
| name: "empty string treated as unset", | ||
| env: map[string]string{"CLAUDE_CODE": ""}, | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "invalid generic value skipped, falls through to next source", | ||
| env: map[string]string{"AI_AGENT": "bad\nvalue", "LLM_AGENT": "gemma4"}, | ||
| want: "gemma4", | ||
| }, | ||
| { | ||
| name: "valid non-ASCII generic value forwarded unchanged", | ||
| env: map[string]string{"AI_AGENT": "claudé-日本"}, | ||
| want: "claudé-日本", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| for _, k := range knownVars { | ||
| t.Setenv(k, "") // isolate from ambient env vars | ||
| } | ||
| for k, v := range tt.env { | ||
| t.Setenv(k, v) | ||
| } | ||
|
|
||
| if got := Detected(); got != tt.want { | ||
| t.Fatalf("Detected() = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValidHeaderValue(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| value string | ||
| want bool | ||
| }{ | ||
| {"plain ascii", "claude-code", true}, | ||
| {"empty string", "", true}, | ||
| {"tab is rejected", "a\tb", false}, | ||
| {"space is rejected", "a b", false}, | ||
| {"utf-8 multibyte", "日本語", true}, | ||
| {"high byte 0xFF", "\xff", true}, | ||
| {"carriage return", "a\rb", false}, | ||
| {"line feed", "a\nb", false}, | ||
| {"null byte", "a\x00b", false}, | ||
| {"del", "a\x7fb", false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := validHeaderValue(tt.value); got != tt.want { | ||
| t.Fatalf("validHeaderValue(%q) = %v, want %v", tt.value, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.