GitLabgitlab-org

cli

main
ply6 git calls264 ms98 KiB4 cachedprotocol v2
314 lines8.9 KiBRaw
1package api
3import (
4 "os"
5 "testing"
7 "github.com/stretchr/testify/assert"
8)
10func TestDetectCodingAgent(t *testing.T) {
11 tests := []struct {
12 name string
13 envVars map[string]string
14 expected string
15 }{
16 {
17 name: "no agent detected",
18 envVars: map[string]string{},
19 expected: "",
20 },
21 {
22 name: "AI_AGENT takes priority over all others",
23 envVars: map[string]string{"AI_AGENT": "claude-code_2-1-140_agent", "CLAUDECODE": "1", "CURSOR_AGENT": "1"},
24 expected: "claude-code_2-1-140_agent",
25 },
26 {
27 name: "AI_AGENT passes through raw value",
28 envVars: map[string]string{"AI_AGENT": "some-custom-agent"},
29 expected: "some-custom-agent",
30 },
31 {
32 name: "Claude Code",
33 envVars: map[string]string{"CLAUDECODE": "1"},
34 expected: "claude-code",
35 },
36 {
37 name: "CLAUDECODE wrong value ignored",
38 envVars: map[string]string{"CLAUDECODE": "true"},
39 expected: "",
40 },
41 {
42 name: "Codex CLI",
43 envVars: map[string]string{"CODEX_THREAD_ID": "thread_abc123"},
44 expected: "codex",
45 },
46 {
47 name: "OpenCode",
48 envVars: map[string]string{"OPENCODE": "1"},
49 expected: "opencode",
50 },
51 {
52 name: "Cursor",
53 envVars: map[string]string{"CURSOR_AGENT": "1"},
54 expected: "cursor",
55 },
56 {
57 name: "Gemini CLI",
58 envVars: map[string]string{"GEMINI_CLI": "1"},
59 expected: "gemini",
60 },
61 {
62 name: "Roo Code",
63 envVars: map[string]string{"ROO_CLI_RUNTIME": "1"},
64 expected: "roo-code",
65 },
66 {
67 name: "ROO_CLI_RUNTIME wrong value ignored",
68 envVars: map[string]string{"ROO_CLI_RUNTIME": "true"},
69 expected: "",
70 },
71 {
72 name: "TERM_PROGRAM=cursor falls back to cursor-terminal",
73 envVars: map[string]string{"TERM_PROGRAM": "cursor"},
74 expected: "cursor-terminal",
75 },
76 {
77 name: "TERM_PROGRAM=kiro falls back to kiro-terminal",
78 envVars: map[string]string{"TERM_PROGRAM": "kiro"},
79 expected: "kiro-terminal",
80 },
81 {
82 name: "TERM_PROGRAM=Kiro is case-insensitive",
83 envVars: map[string]string{"TERM_PROGRAM": "Kiro"},
84 expected: "kiro-terminal",
85 },
86 {
87 name: "TERM_PROGRAM=kiroshi is ignored",
88 envVars: map[string]string{"TERM_PROGRAM": "kiroshi"},
89 expected: "",
90 },
91 {
92 name: "TERM_PROGRAM=Windsurf is case-insensitive",
93 envVars: map[string]string{"TERM_PROGRAM": "Windsurf"},
94 expected: "windsurf-terminal",
95 },
96 {
97 name: "TERM_PROGRAM=zed falls back to zed-terminal",
98 envVars: map[string]string{"TERM_PROGRAM": "zed"},
99 expected: "zed-terminal",
100 },
101 {
102 name: "TERM_PROGRAM=ghostty is ignored",
103 envVars: map[string]string{"TERM_PROGRAM": "ghostty"},
104 expected: "",
105 },
106 {
107 name: "CURSOR_AGENT wins over TERM_PROGRAM=cursor",
108 envVars: map[string]string{"CURSOR_AGENT": "1", "TERM_PROGRAM": "cursor"},
109 expected: "cursor",
110 },
111 {
112 name: "AI_AGENT with spaces is ignored",
113 envVars: map[string]string{"AI_AGENT": "has spaces", "CLAUDECODE": "1"},
114 expected: "claude-code",
115 },
116 {
117 name: "AI_AGENT with special chars is ignored",
118 envVars: map[string]string{"AI_AGENT": "agent/name!@#"},
119 expected: "",
120 },
121 {
122 name: "AI_AGENT exceeding max length is ignored",
123 envVars: map[string]string{"AI_AGENT": "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789XYZ"},
124 expected: "",
125 },
126 {
127 name: "AI_AGENT at exactly max length is accepted",
128 envVars: map[string]string{"AI_AGENT": "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789"},
129 expected: "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789",
130 },
131 {
132 name: "Copilot CLI",
133 envVars: map[string]string{"COPILOT_CLI": "1"},
134 expected: "copilot",
135 },
136 {
137 name: "Copilot CLI with value true is ignored",
138 envVars: map[string]string{"COPILOT_CLI": "true"},
139 expected: "",
140 },
141 {
142 name: "Copilot CLI with value 0 is ignored",
143 envVars: map[string]string{"COPILOT_CLI": "0"},
144 expected: "",
145 },
146 {
147 name: "AI_AGENT=1 names no agent",
148 envVars: map[string]string{"AI_AGENT": "1"},
149 expected: "",
150 },
151 {
152 name: "AI_AGENT=true names no agent and falls through",
153 envVars: map[string]string{"AI_AGENT": "true", "CLAUDECODE": "1"},
154 expected: "claude-code",
155 },
156 {
157 name: "AI_AGENT boolean words are matched in any case",
158 envVars: map[string]string{"AI_AGENT": "TRUE"},
159 expected: "",
160 },
161 {
162 name: "AI_AGENT=false names no agent",
163 envVars: map[string]string{"AI_AGENT": "false"},
164 expected: "",
165 },
166 {
167 name: "AI_AGENT=0 names no agent",
168 envVars: map[string]string{"AI_AGENT": "0"},
169 expected: "",
170 },
171 {
172 name: "Kilo Code wins over the OpenCode markers it inherits",
173 envVars: map[string]string{"KILO": "1", "OPENCODE": "1"},
174 expected: "kilo-code",
175 },
176 {
177 name: "KILO with value true is ignored",
178 envVars: map[string]string{"KILO": "true", "OPENCODE": "1"},
179 expected: "opencode",
180 },
181 {
182 name: "Cline",
183 envVars: map[string]string{"CLINE_ACTIVE": "true"},
184 expected: "cline",
185 },
186 {
187 name: "CLINE_ACTIVE with value 1 is ignored",
188 envVars: map[string]string{"CLINE_ACTIVE": "1"},
189 expected: "",
190 },
191 {
192 name: "Goose",
193 envVars: map[string]string{"GOOSE_TERMINAL": "1"},
194 expected: "goose",
195 },
196 {
197 name: "GOOSE_TERMINAL with value true is ignored",
198 envVars: map[string]string{"GOOSE_TERMINAL": "true"},
199 expected: "",
200 },
201 {
202 name: "Kiro CLI wins over TERM_PROGRAM=kiro",
203 envVars: map[string]string{"KIRO_CLI_IS_CHILD_PROCESS": "1", "TERM_PROGRAM": "kiro"},
204 expected: "kiro-cli",
205 },
206 {
207 name: "Amazon Q Developer CLI",
208 envVars: map[string]string{"AWS_EXECUTION_ENV": "AmazonQ-For-CLI Version/1.16.0"},
209 expected: "amazon-q",
210 },
211 {
212 name: "Kiro CLI wins over the Amazon Q marker it also sets",
213 envVars: map[string]string{"KIRO_CLI_IS_CHILD_PROCESS": "1", "AWS_EXECUTION_ENV": "AmazonQ-For-CLI Version/3.4.0 acp-client/kiro-cli-non-interactive"},
214 expected: "kiro-cli",
215 },
216 {
217 name: "Other AWS_EXECUTION_ENV values are ignored",
218 envVars: map[string]string{"AWS_EXECUTION_ENV": "AWS_Lambda_go1.x"},
219 expected: "",
220 },
221 {
222 name: "Kiro IDE agent wins over TERM_PROGRAM=kiro",
223 envVars: map[string]string{"KIRO_SESSION_ID": "sess_123", "TERM_PROGRAM": "kiro"},
224 expected: "kiro",
225 },
226 {
227 name: "Kiro IDE agent without TERM_PROGRAM",
228 envVars: map[string]string{"KIRO_SESSION_ID": "sess_123"},
229 expected: "kiro",
230 },
231 {
232 name: "Kiro CLI wins over the session ID it also sets",
233 envVars: map[string]string{"KIRO_CLI_IS_CHILD_PROCESS": "1", "KIRO_SESSION_ID": "sess_123"},
234 expected: "kiro-cli",
235 },
236 {
237 name: "Junie CLI",
238 envVars: map[string]string{"JUNIE_SHIM_PATH": "/Users/me/.local/bin/junie"},
239 expected: "junie",
240 },
241 {
242 name: "Warp cloud agent run",
243 envVars: map[string]string{"OZ_RUN_ID": "run_123"},
244 expected: "warp",
245 },
246 {
247 name: "Qwen Code",
248 envVars: map[string]string{"QWEN_CODE": "1"},
249 expected: "qwen-code",
250 },
251 {
252 name: "QWEN_CODE with value true is ignored",
253 envVars: map[string]string{"QWEN_CODE": "true"},
254 expected: "",
255 },
256 {
257 name: "Roo Code extension terminal",
258 envVars: map[string]string{"ROO_ACTIVE": "true"},
259 expected: "roo-code",
260 },
261 {
262 name: "ROO_ACTIVE with value 1 is ignored",
263 envVars: map[string]string{"ROO_ACTIVE": "1"},
264 expected: "",
265 },
266 {
267 name: "Zed agent terminal",
268 envVars: map[string]string{"TERM_PROGRAM": "zed", "PAGER": "", "GIT_PAGER": "cat"},
269 expected: "zed",
270 },
271 {
272 name: "Zed terminal with PAGER unset",
273 envVars: map[string]string{"TERM_PROGRAM": "zed", "GIT_PAGER": "cat"},
274 expected: "zed-terminal",
275 },
276 {
277 name: "Zed terminal with PAGER empty and GIT_PAGER unset",
278 envVars: map[string]string{"TERM_PROGRAM": "zed", "PAGER": ""},
279 expected: "zed-terminal",
280 },
281 {
282 name: "Zed terminal with PAGER empty and a user GIT_PAGER",
283 envVars: map[string]string{"TERM_PROGRAM": "zed", "PAGER": "", "GIT_PAGER": "delta"},
284 expected: "zed-terminal",
285 },
286 {
287 name: "Zed terminal with a user pager",
288 envVars: map[string]string{"TERM_PROGRAM": "zed", "PAGER": "less", "GIT_PAGER": "cat"},
289 expected: "zed-terminal",
290 },
291 }
293 allAgentVars := []string{
294 "AI_AGENT", "CLAUDECODE", "CODEX_THREAD_ID", "COPILOT_CLI",
295 "OPENCODE", "CURSOR_AGENT", "GEMINI_CLI", "KILO",
296 "CLINE_ACTIVE", "GOOSE_TERMINAL", "KIRO_CLI_IS_CHILD_PROCESS", "QWEN_CODE", "ROO_ACTIVE", "AWS_EXECUTION_ENV", "JUNIE_SHIM_PATH", "OZ_RUN_ID", "KIRO_SESSION_ID",
297 "ROO_CLI_RUNTIME", "TERM_PROGRAM", "PAGER", "GIT_PAGER",
298 }
300 for _, tt := range tests {
301 t.Run(tt.name, func(t *testing.T) {
302 for _, key := range allAgentVars {
303 t.Setenv(key, "") // restores the original value after the test
304 _ = os.Unsetenv(key)
305 }
306 for key, value := range tt.envVars {
307 t.Setenv(key, value)
308 }
310 result := DetectCodingAgent()
311 assert.Equal(t, tt.expected, result)
312 })
313 }
Diagnostics6 git calls · 264 ms
CallStatusSizeTime0264 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:02001.1 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=1165
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1165
git.pkt.payload.max=1138
git.sideband.band1.bytes=1138
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1138
git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141c
Open pack map
fetch2001.8 KiB254 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=254
git.response.bytes=1868
git.response.cache=MISS
git.response.duration_ms=254
git.fetch.wants=46fe3492deb0ae3aecc56bf9672077974a135d94
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1868
git.pkt.payload.max=1841
git.sideband.band1.bytes=1841
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e1226eaeea7fd8
Request
f3ac9c81…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format