22
33import com .databricks .sdk .core .utils .Environment ;
44import java .io .File ;
5- import java .util .AbstractMap ;
65import java .util .ArrayList ;
76import java .util .Arrays ;
87import java .util .Collections ;
98import java .util .List ;
10- import java .util .Map ;
119import java .util .regex .Pattern ;
1210import java .util .stream .Collectors ;
1311
@@ -132,7 +130,7 @@ public static String asString() {
132130 segments .add (String .format ("cicd/%s" , cicdProvider ));
133131 }
134132 String agent = agentProvider ();
135- if (agent != null && !agent .isEmpty ()) {
133+ if (!agent .isEmpty ()) {
136134 segments .add (String .format ("agent/%s" , agent ));
137135 }
138136 // Concurrent iteration over ArrayList must be guarded with synchronized.
@@ -239,21 +237,28 @@ private static String cicdProvider() {
239237 return cicdProvider ;
240238 }
241239
240+ // Maps an environment variable to an agent product name.
241+ private static class AgentDef {
242+ private final String envVar ;
243+ private final String product ;
244+
245+ AgentDef (String envVar , String product ) {
246+ this .envVar = envVar ;
247+ this .product = product ;
248+ }
249+ }
250+
242251 // Canonical list of known AI coding agents.
243252 // Keep this list in sync with databricks-sdk-go and databricks-sdk-py.
244- private static List <Map . Entry < String , String > > listKnownAgents () {
253+ private static List <AgentDef > listKnownAgents () {
245254 return Arrays .asList (
246- new AbstractMap .SimpleEntry <>("ANTIGRAVITY_AGENT" , "antigravity" ), // Closed source (Google)
247- new AbstractMap .SimpleEntry <>(
248- "CLAUDECODE" , "claude-code" ), // https://github.com/anthropics/claude-code
249- new AbstractMap .SimpleEntry <>(
250- "CLINE_ACTIVE" , "cline" ), // https://github.com/cline/cline (v3.24.0+)
251- new AbstractMap .SimpleEntry <>("CODEX_CI" , "codex" ), // https://github.com/openai/codex
252- new AbstractMap .SimpleEntry <>("CURSOR_AGENT" , "cursor" ), // Closed source
253- new AbstractMap .SimpleEntry <>(
254- "GEMINI_CLI" , "gemini-cli" ), // https://google-gemini.github.io/gemini-cli
255- new AbstractMap .SimpleEntry <>(
256- "OPENCODE" , "opencode" )); // https://github.com/opencode-ai/opencode
255+ new AgentDef ("ANTIGRAVITY_AGENT" , "antigravity" ), // Closed source (Google)
256+ new AgentDef ("CLAUDECODE" , "claude-code" ), // https://github.com/anthropics/claude-code
257+ new AgentDef ("CLINE_ACTIVE" , "cline" ), // https://github.com/cline/cline (v3.24.0+)
258+ new AgentDef ("CODEX_CI" , "codex" ), // https://github.com/openai/codex
259+ new AgentDef ("CURSOR_AGENT" , "cursor" ), // Closed source
260+ new AgentDef ("GEMINI_CLI" , "gemini-cli" ), // https://google-gemini.github.io/gemini-cli
261+ new AgentDef ("OPENCODE" , "opencode" )); // https://github.com/opencode-ai/opencode
257262 }
258263
259264 // Looks up the active agent provider based on environment variables.
@@ -262,11 +267,14 @@ private static List<Map.Entry<String, String>> listKnownAgents() {
262267 private static String lookupAgentProvider (Environment env ) {
263268 String detected = "" ;
264269 int count = 0 ;
265- for (Map . Entry < String , String > agent : listKnownAgents ()) {
266- String value = env .get (agent .getKey () );
270+ for (AgentDef agent : listKnownAgents ()) {
271+ String value = env .get (agent .envVar );
267272 if (value != null && !value .isEmpty ()) {
268- detected = agent .getValue () ;
273+ detected = agent .product ;
269274 count ++;
275+ if (count > 1 ) {
276+ return "" ;
277+ }
270278 }
271279 }
272280 if (count == 1 ) {
0 commit comments