detect-coding-agent 0.1.0

Detect if your application is being invoked by an AI coding agent such as Claude Code, Copilot, Cursor, Codex, Aider, and many more.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use crate::env::Env;
use crate::types::{AgentKind, DetectedAgent};

/// A provider definition: a static description of an AI coding agent and
/// the function used to detect its presence in the environment.
pub(crate) struct Provider {
    pub id: &'static str,
    pub name: &'static str,
    pub kind: AgentKind,
    /// Returns `true` when the provider is detected in `env`.
    pub matches: fn(&Env) -> bool,
}

impl Provider {
    fn detect(&self, env: &Env) -> Option<DetectedAgent> {
        if (self.matches)(env) {
            Some(DetectedAgent {
                id: self.id,
                name: self.name,
                kind: self.kind.clone(),
            })
        } else {
            None
        }
    }
}

// ---------------------------------------------------------------------------
// Individual matcher functions
// ---------------------------------------------------------------------------

fn matches_opencode(env: &Env) -> bool {
    // OpenCode sets any of these variables on shell executions.
    // https://opencode.ai
    env.contains("OPENCODE")
        || env.contains("OPENCODE_BIN_PATH")
        || env.contains("OPENCODE_SERVER")
        || env.contains("OPENCODE_APP_INFO")
        || env.contains("OPENCODE_MODES")
}

fn matches_jules(env: &Env) -> bool {
    // Jules (Google) runs as user "swebot" with HOME=/home/jules.
    env.equals("HOME", "/home/jules") && env.equals("USER", "swebot")
}

fn matches_claude_code(env: &Env) -> bool {
    // Claude Code (Anthropic) sets CLAUDECODE in spawned shells.
    // https://docs.anthropic.com/en/docs/claude-code
    env.contains("CLAUDECODE")
}

fn matches_cursor_agent(env: &Env) -> bool {
    // Cursor in agent mode sets CURSOR_TRACE_ID AND overrides PAGER.
    // The PAGER override is the distinguishing signal for the automated agent
    // versus an interactive Cursor terminal session.
    env.contains("CURSOR_TRACE_ID") && env.equals("PAGER", "head -n 10000 | cat")
}

fn matches_cursor(env: &Env) -> bool {
    // Cursor IDE interactive terminal: CURSOR_TRACE_ID without the PAGER override.
    env.contains("CURSOR_TRACE_ID") && !env.equals("PAGER", "head -n 10000 | cat")
}

fn matches_antigravity(env: &Env) -> bool {
    // Antigravity coding agent.
    env.contains("ANTIGRAVITY_AGENT") || env.contains("ANTIGRAVITY_PROJECT_ID")
}

fn matches_gemini_cli(env: &Env) -> bool {
    // Gemini CLI (Google) sets GEMINI_CLI=1 on every shell command and MCP server it spawns.
    // https://github.com/google-gemini/gemini-cli
    env.equals("GEMINI_CLI", "1")
}

fn matches_codex(env: &Env) -> bool {
    // OpenAI Codex injects CODEX_THREAD_ID (the session conversation id) into every shell command.
    // https://github.com/openai/codex/blob/main/codex-rs/protocol/src/shell_environment.rs
    env.contains("CODEX_THREAD_ID")
}

fn matches_replit_assistant(env: &Env) -> bool {
    // Replit AI Assistant mode: REPL_ID set AND REPLIT_MODE=assistant.
    env.contains("REPL_ID") && env.equals("REPLIT_MODE", "assistant")
}

fn matches_replit(env: &Env) -> bool {
    // Replit interactive environment: REPL_ID without the assistant override.
    env.contains("REPL_ID") && !env.equals("REPLIT_MODE", "assistant")
}

fn matches_aider(env: &Env) -> bool {
    // Aider sets AIDER_API_KEY in its shell environment.
    // https://aider.chat
    env.contains("AIDER_API_KEY")
}

fn matches_bolt_agent(env: &Env) -> bool {
    // Bolt.new in agent mode: /bin/jsh shell AND npm_config_yes is set.
    env.equals("SHELL", "/bin/jsh") && env.contains("npm_config_yes")
}

fn matches_bolt(env: &Env) -> bool {
    // Bolt.new interactive environment: /bin/jsh shell WITHOUT npm_config_yes.
    env.equals("SHELL", "/bin/jsh") && !env.contains("npm_config_yes")
}

fn matches_zed_agent(env: &Env) -> bool {
    // Zed editor in agent mode: TERM_PROGRAM=zed AND PAGER=cat (disables pagination).
    env.equals("TERM_PROGRAM", "zed") && env.equals("PAGER", "cat")
}

fn matches_zed(env: &Env) -> bool {
    // Zed editor interactive: TERM_PROGRAM=zed WITHOUT the PAGER override.
    env.equals("TERM_PROGRAM", "zed") && !env.equals("PAGER", "cat")
}

fn matches_windsurf(env: &Env) -> bool {
    // Windsurf (Codeium) sets CODEIUM_EDITOR_APP_ROOT.
    // https://windsurf.com
    env.contains("CODEIUM_EDITOR_APP_ROOT")
}

fn matches_crush(env: &Env) -> bool {
    // Crush (Charm) sets CRUSH=1 (and AGENT=crush, AI_AGENT=crush) on every shell exec.
    // https://github.com/charmbracelet/crush
    env.equals("CRUSH", "1") || env.equals("AGENT", "crush") || env.equals("AI_AGENT", "crush")
}

fn matches_amp(env: &Env) -> bool {
    // Sourcegraph Amp injects AGENT=amp or AMP_CURRENT_THREAD_ID into shell tool executions.
    // https://ampcode.com
    env.contains("AMP_CURRENT_THREAD_ID") || env.equals("AGENT", "amp")
}

fn matches_auggie(env: &Env) -> bool {
    // Augment Code's Auggie CLI sets AUGMENT_AGENT=1 in the shell tool's env.
    // https://augmentcode.com
    env.equals("AUGMENT_AGENT", "1")
}

fn matches_qwen_code(env: &Env) -> bool {
    // Qwen Code (Alibaba, Gemini CLI fork) sets QWEN_CODE=1 on every shell exec.
    env.equals("QWEN_CODE", "1")
}

fn matches_copilot_cloud_agent(env: &Env) -> bool {
    // GitHub Copilot Cloud Agent (Copilot coding agent running as a GitHub Actions job).
    // It sets COPILOT_AGENT_SESSION_ID and/or COPILOT_CLI=1 together with GITHUB_ACTIONS.
    (env.contains("COPILOT_AGENT_SESSION_ID")
        || env.contains("COPILOT_AGENT_JOB_ID")
        || env.equals("COPILOT_CLI", "1"))
        && env.equals("GITHUB_ACTIONS", "true")
}

fn matches_copilot_vscode(env: &Env) -> bool {
    // GitHub Copilot agent mode inside VS Code: TERM_PROGRAM=vscode AND GIT_PAGER=cat.
    // The GIT_PAGER override is the distinguishing signal from an ordinary VS Code terminal.
    env.equals("TERM_PROGRAM", "vscode") && env.equals("GIT_PAGER", "cat")
}

fn matches_warp(env: &Env) -> bool {
    // Warp terminal (hybrid: interactive + AI features).
    env.equals("TERM_PROGRAM", "WarpTerminal")
}

// ---------------------------------------------------------------------------
// Provider registry
// ---------------------------------------------------------------------------

/// Returns the ordered list of known AI coding agent providers.
///
/// Detection is attempted in this order; the **first match wins**.  More
/// specific checks (e.g. "agent with extra env flag") are placed before their
/// less specific counterparts (e.g. "same tool, interactive mode").
pub(crate) fn all_providers() -> &'static [Provider] {
    static PROVIDERS: std::sync::OnceLock<Vec<Provider>> = std::sync::OnceLock::new();
    PROVIDERS.get_or_init(|| {
        vec![
            // --- Agents that have unique, unambiguous environment signals ---
            Provider {
                id: "opencode",
                name: "OpenCode",
                kind: AgentKind::Agent,
                matches: matches_opencode,
            },
            Provider {
                id: "jules",
                name: "Jules",
                kind: AgentKind::Agent,
                matches: matches_jules,
            },
            Provider {
                id: "claude-code",
                name: "Claude Code",
                kind: AgentKind::Agent,
                matches: matches_claude_code,
            },
            Provider {
                id: "gemini-cli",
                name: "Gemini CLI",
                kind: AgentKind::Agent,
                matches: matches_gemini_cli,
            },
            Provider {
                id: "codex",
                name: "OpenAI Codex",
                kind: AgentKind::Agent,
                matches: matches_codex,
            },
            Provider {
                id: "aider",
                name: "Aider",
                kind: AgentKind::Agent,
                matches: matches_aider,
            },
            Provider {
                id: "windsurf",
                name: "Windsurf",
                kind: AgentKind::Agent,
                matches: matches_windsurf,
            },
            Provider {
                id: "antigravity",
                name: "Antigravity",
                kind: AgentKind::Agent,
                matches: matches_antigravity,
            },
            Provider {
                id: "crush",
                name: "Crush",
                kind: AgentKind::Agent,
                matches: matches_crush,
            },
            Provider {
                id: "amp",
                name: "Amp",
                kind: AgentKind::Agent,
                matches: matches_amp,
            },
            Provider {
                id: "auggie",
                name: "Auggie",
                kind: AgentKind::Agent,
                matches: matches_auggie,
            },
            Provider {
                id: "qwen-code",
                name: "Qwen Code",
                kind: AgentKind::Agent,
                matches: matches_qwen_code,
            },
            // GitHub Copilot: cloud agent check must come before VS Code check
            Provider {
                id: "copilot-cloud-agent",
                name: "GitHub Copilot Cloud Agent",
                kind: AgentKind::Agent,
                matches: matches_copilot_cloud_agent,
            },
            // --- Agents that share an env var with an interactive mode ---
            // More specific (agent) checks first, less specific (interactive) after.
            Provider {
                id: "cursor-agent",
                name: "Cursor Agent",
                kind: AgentKind::Agent,
                matches: matches_cursor_agent,
            },
            Provider {
                id: "cursor",
                name: "Cursor",
                kind: AgentKind::Interactive,
                matches: matches_cursor,
            },
            Provider {
                id: "replit-assistant",
                name: "Replit Assistant",
                kind: AgentKind::Agent,
                matches: matches_replit_assistant,
            },
            Provider {
                id: "replit",
                name: "Replit",
                kind: AgentKind::Interactive,
                matches: matches_replit,
            },
            Provider {
                id: "bolt-agent",
                name: "Bolt.new Agent",
                kind: AgentKind::Agent,
                matches: matches_bolt_agent,
            },
            Provider {
                id: "bolt",
                name: "Bolt.new",
                kind: AgentKind::Interactive,
                matches: matches_bolt,
            },
            Provider {
                id: "zed-agent",
                name: "Zed Agent",
                kind: AgentKind::Agent,
                matches: matches_zed_agent,
            },
            Provider {
                id: "zed",
                name: "Zed",
                kind: AgentKind::Interactive,
                matches: matches_zed,
            },
            Provider {
                id: "copilot-vscode",
                name: "GitHub Copilot in VS Code",
                kind: AgentKind::Agent,
                matches: matches_copilot_vscode,
            },
            // --- Hybrid environments ---
            Provider {
                id: "warp",
                name: "Warp Terminal",
                kind: AgentKind::Hybrid,
                matches: matches_warp,
            },
        ]
    })
}

/// Run detection against the provided `env`, returning the first matching provider.
pub(crate) fn detect(env: &Env) -> Option<DetectedAgent> {
    all_providers()
        .iter()
        .find_map(|provider| provider.detect(env))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::env::EnvBuilder;

    fn env_with(key: &str, value: &str) -> Env {
        EnvBuilder::new().set(key, value).build()
    }

    fn env_with2(k1: &str, v1: &str, k2: &str, v2: &str) -> Env {
        EnvBuilder::new().set(k1, v1).set(k2, v2).build()
    }

    #[test]
    fn detects_opencode() {
        let env = env_with("OPENCODE", "1");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "opencode");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_opencode_by_bin_path() {
        let env = env_with("OPENCODE_BIN_PATH", "/usr/local/bin/opencode");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "opencode");
    }

    #[test]
    fn detects_jules() {
        let env = env_with2("HOME", "/home/jules", "USER", "swebot");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "jules");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn does_not_detect_jules_wrong_user() {
        let env = env_with2("HOME", "/home/jules", "USER", "notjules");
        assert!(detect(&env).is_none());
    }

    #[test]
    fn detects_claude_code() {
        let env = env_with("CLAUDECODE", "1");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "claude-code");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_gemini_cli() {
        let env = env_with("GEMINI_CLI", "1");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "gemini-cli");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn does_not_detect_gemini_cli_wrong_value() {
        let env = env_with("GEMINI_CLI", "0");
        assert!(detect(&env).is_none());
    }

    #[test]
    fn detects_codex() {
        let env = env_with("CODEX_THREAD_ID", "thread-abc-123");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "codex");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_aider() {
        let env = env_with("AIDER_API_KEY", "sk-abc123");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "aider");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_windsurf() {
        let env = env_with("CODEIUM_EDITOR_APP_ROOT", "/opt/windsurf");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "windsurf");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_antigravity_by_agent_var() {
        let env = env_with("ANTIGRAVITY_AGENT", "true");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "antigravity");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_antigravity_by_project_id() {
        let env = env_with("ANTIGRAVITY_PROJECT_ID", "proj-abc");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "antigravity");
    }

    #[test]
    fn detects_crush_by_crush_var() {
        let env = env_with("CRUSH", "1");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "crush");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_crush_by_agent_var() {
        let env = env_with("AGENT", "crush");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "crush");
    }

    #[test]
    fn detects_amp_by_thread_id() {
        let env = env_with("AMP_CURRENT_THREAD_ID", "thread-xyz");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "amp");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_amp_by_agent_var() {
        let env = env_with("AGENT", "amp");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "amp");
    }

    #[test]
    fn detects_auggie() {
        let env = env_with("AUGMENT_AGENT", "1");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "auggie");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_qwen_code() {
        let env = env_with("QWEN_CODE", "1");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "qwen-code");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_copilot_cloud_agent() {
        let env = env_with2(
            "COPILOT_AGENT_SESSION_ID",
            "sess-abc",
            "GITHUB_ACTIONS",
            "true",
        );
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "copilot-cloud-agent");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_copilot_cloud_agent_by_cli_flag() {
        let env = env_with2("COPILOT_CLI", "1", "GITHUB_ACTIONS", "true");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "copilot-cloud-agent");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn does_not_detect_copilot_cloud_without_github_actions() {
        let env = env_with("COPILOT_AGENT_SESSION_ID", "sess-abc");
        assert!(detect(&env).is_none());
    }

    #[test]
    fn detects_cursor_agent() {
        let env = env_with2(
            "CURSOR_TRACE_ID",
            "trace-abc",
            "PAGER",
            "head -n 10000 | cat",
        );
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "cursor-agent");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_cursor_interactive() {
        let env = env_with("CURSOR_TRACE_ID", "trace-abc");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "cursor");
        assert_eq!(result.kind, AgentKind::Interactive);
    }

    #[test]
    fn detects_replit_assistant() {
        let env = env_with2("REPL_ID", "repl-123", "REPLIT_MODE", "assistant");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "replit-assistant");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_replit_interactive() {
        let env = env_with("REPL_ID", "repl-123");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "replit");
        assert_eq!(result.kind, AgentKind::Interactive);
    }

    #[test]
    fn detects_bolt_agent() {
        let env = env_with2("SHELL", "/bin/jsh", "npm_config_yes", "true");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "bolt-agent");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_bolt_interactive() {
        let env = env_with("SHELL", "/bin/jsh");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "bolt");
        assert_eq!(result.kind, AgentKind::Interactive);
    }

    #[test]
    fn detects_zed_agent() {
        let env = env_with2("TERM_PROGRAM", "zed", "PAGER", "cat");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "zed-agent");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_zed_interactive() {
        let env = env_with("TERM_PROGRAM", "zed");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "zed");
        assert_eq!(result.kind, AgentKind::Interactive);
    }

    #[test]
    fn detects_copilot_vscode_agent() {
        let env = env_with2("TERM_PROGRAM", "vscode", "GIT_PAGER", "cat");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "copilot-vscode");
        assert_eq!(result.kind, AgentKind::Agent);
    }

    #[test]
    fn detects_warp() {
        let env = env_with("TERM_PROGRAM", "WarpTerminal");
        let result = detect(&env).unwrap();
        assert_eq!(result.id, "warp");
        assert_eq!(result.kind, AgentKind::Hybrid);
    }

    #[test]
    fn no_detection_in_empty_env() {
        let env = Env::from_map(std::collections::HashMap::new());
        assert!(detect(&env).is_none());
    }

    #[test]
    fn no_detection_for_unrelated_env() {
        let env = env_with("PATH", "/usr/bin:/bin");
        assert!(detect(&env).is_none());
    }
}