From c14f258907252cc4b44e79b7f92e2cc870bb6259 Mon Sep 17 00:00:00 2001 From: James Russo Date: Mon, 9 Nov 2026 01:38:58 +0000 Subject: [PATCH] feat(cli): detect more agent runtimes in telemetry (#7423) * feat(cli): detect more agent runtimes in telemetry Agent runtime rules: - copilot_agent: key on COPILOT_AGENT=1 or AI_AGENT=github_copilot_* (VS Code agent terminals) instead of the unverified Actions markers. - copilot_cli: COPILOT_CLI=1 or COPILOT_AGENT_SESSION_ID, ahead of copilot_agent. - kilo (KILO=1, KILOCODE_VERSION), ahead of opencode. - opencode (OPENCODE=1, OPENCODE_SESSION_ID). - qwen_code (QWEN_CODE=1), junie (JUNIE_SHIM_PATH), antigravity (ANTIGRAVITY_AGENT), qoder (QODER_AGENT), kiro (AGENT_DISPLAY_OUT, AGENT_CONTEXT_OUT), openhands (AI_AGENT=openhands). - AI_AGENT fallback after every vendor rule: known names map to their runtime, any other slug-safe name becomes other_ai_agent. Bare AGENT is not used. Discovery hints: other_ai_agent keeps its hints, agent_hint prefers AI_AGENT over AGENT, and AGENT_TOOLSDIRECTORY, GSM_SKIP_SSH_AGENT_WORKAROUND and EDGE_METRICS_SESSION_ID are no longer hint keys. CI: name AWS CodeBuild (CODEBUILD_BUILD_ID) as codebuild. Co-Authored-By: Claude Opus 5.5 (1M context) * feat(cli): detect agents seen in discovery hints Add rules for closed-source agents whose markers show up in the CLI's own agent_env_hints on non-interactive runs: WorkBuddy, CodeBuddy, Trae, Pippit, Doubao, Aily, Grok, Kimi, ZCode, SWE-agent and apps built on the Claude Agent SDK. Qoder also matches its CLI and agent SDK keys. The AI_AGENT name map now covers the names agents report (Devin, Amp, v0, Rovo Dev, Base44, Trae, Antigravity, OpenHands, Copilot, and hermes-agent), and a known name in the bare AGENT variable counts too. Unknown AGENT values such as AGENT=1 still do not. Co-Authored-By: Claude Opus 5.5 (1M context) * feat(cli): detect the DeepSeek harness Every dsh model shell call runs with DSH_SHELL=1 and the agent's DSH_SESSION_ID. HyperFrames Desktop also runs dsh; its runs carry HYPERFRAMES_CLIENT, so the client field separates the two. Co-Authored-By: Claude Opus 5.5 (1M context) * docs(cli): cite the Agent SDK source for claude_agent_sdk Co-Authored-By: Claude Opus 5.5 (1M context) * fix(cli): let the closest agent win over inherited markers Copilot's COPILOT_AGENT / AI_AGENT and COPILOT_CLI pass down to the agents it starts, and TERM_PROGRAM / REPL_ID only say where a shell runs. Those rules now come after every other agent rule, so opencode or Qwen started by Copilot, or Kilo in a Cursor terminal, report as themselves. dsh keys on DSH_SHELL=1 only: it sets DSH_SESSION_ID on the terminal a person types into too. Qoder drops its app-wide SDK and setting keys, CodeBuddy drops CODEBUDDY_AGENT_RUNTIME (a setting it reads), and Kiro's markers must be non-empty. Co-Authored-By: Claude Opus 5.5 (1M context) --------- Co-authored-by: Claude Opus 5.5 (1M context) --- .../cli/src/telemetry/agent_runtime.test.ts | 290 ++++++++++++++- packages/cli/src/telemetry/agent_runtime.ts | 336 ++++++++++++++---- packages/cli/src/telemetry/system.test.ts | 47 +++ packages/cli/src/telemetry/system.ts | 5 +- .../cli/src/utils/attendedTerminal.test.ts | 2 +- 5 files changed, 611 insertions(+), 69 deletions(-) diff --git a/packages/cli/src/telemetry/agent_runtime.test.ts b/packages/cli/src/telemetry/agent_runtime.test.ts index 29f130a51c..62e295e938 100644 --- a/packages/cli/src/telemetry/agent_runtime.test.ts +++ b/packages/cli/src/telemetry/agent_runtime.test.ts @@ -27,6 +27,22 @@ const VENDOR_ENV_KEYS = [ "CLINE_ACTIVE", "GEMINI_CLI", "CRUSH", + "COPILOT_AGENT", + "COPILOT_CLI", + "COPILOT_AGENT_SESSION_ID", + "KILO", + "KILOCODE_VERSION", + "OPENCODE", + "OPENCODE_SESSION_ID", + "QWEN_CODE", + "JUNIE_SHIM_PATH", + "ANTIGRAVITY_AGENT", + "QODER_AGENT", + "AGENT_DISPLAY_OUT", + "AGENT_CONTEXT_OUT", + "DSH_SHELL", + "AGENT", + "AI_AGENT", ] as const; function stripVendorEnv(): void { @@ -186,13 +202,43 @@ describe("detectAgentRuntime — Cursor / Copilot / cohort", () => { expect(detectAgentRuntime()).toBe("cursor"); }); - it("detects Copilot Coding Agent via GITHUB_ACTIONS + COPILOT_AGENT_ID", async () => { - process.env["GITHUB_ACTIONS"] = "true"; - process.env["COPILOT_AGENT_ID"] = "abc123"; + it("detects Copilot agent mode via COPILOT_AGENT=1 (VS Code agent terminals)", async () => { + process.env["COPILOT_AGENT"] = "1"; const { detectAgentRuntime } = await import("./agent_runtime.js"); expect(detectAgentRuntime()).toBe("copilot_agent"); }); + it("detects Copilot agent mode via AI_AGENT=github_copilot_", async () => { + process.env["AI_AGENT"] = "github_copilot_vscode_agent"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("copilot_agent"); + }); + + it("no longer reads the unverified Actions markers as copilot_agent", async () => { + process.env["GITHUB_ACTIONS"] = "true"; + process.env["COPILOT_AGENT_ID"] = "abc123"; + process.env["RUNNER_NAME"] = "Copilot"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + }); + + it.each(["COPILOT_CLI", "COPILOT_AGENT_SESSION_ID"])( + "detects Copilot CLI via %s", + async (key) => { + process.env[key] = "1"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("copilot_cli"); + }, + ); + + it("attributes Copilot CLI run from a VS Code agent terminal to the CLI", async () => { + process.env["COPILOT_AGENT"] = "1"; + process.env["AI_AGENT"] = "github_copilot_vscode_agent"; + process.env["COPILOT_CLI"] = "1"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("copilot_cli"); + }); + it("does NOT flag generic GitHub Actions as copilot_agent", async () => { process.env["GITHUB_ACTIONS"] = "true"; const { detectAgentRuntime } = await import("./agent_runtime.js"); @@ -396,6 +442,222 @@ describe("detectAgentRuntime — Windsurf / Cline / Gemini CLI / Crush", () => { }); }); +describe("detectAgentRuntime: Kilo / opencode / Qwen / Junie / Antigravity / Qoder / Kiro / OpenHands", () => { + const savedEnv = { ...process.env }; + beforeEach(stripVendorEnv); + afterEach(() => { + process.env = { ...savedEnv }; + }); + + it.each([ + ["KILO", "1", "kilo"], + ["KILOCODE_VERSION", "1.0.0", "kilo"], + ["OPENCODE", "1", "opencode"], + ["OPENCODE_SESSION_ID", "ses_1", "opencode"], + ["QWEN_CODE", "1", "qwen_code"], + ["JUNIE_SHIM_PATH", "/usr/local/bin/junie", "junie"], + ["ANTIGRAVITY_AGENT", "1", "antigravity"], + ["QODER_AGENT", "1", "qoder"], + ["AGENT_DISPLAY_OUT", "/tmp/display", "kiro"], + ["AGENT_CONTEXT_OUT", "/tmp/context", "kiro"], + ["AI_AGENT", "openhands", "openhands"], + ])("detects %s=%s as %s", async (key, value, runtime) => { + process.env[key] = value; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe(runtime); + }); + + it("attributes a Kilo shell to kilo, not the opencode markers it also sets", async () => { + process.env["KILO"] = "1"; + process.env["OPENCODE"] = "1"; + process.env["AGENT"] = "1"; + process.env["AI_AGENT"] = "opencode"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("kilo"); + }); + + it.each(["AGENT_DISPLAY_OUT", "AGENT_CONTEXT_OUT"])( + "does not treat an empty %s as Kiro", + async (key) => { + process.env[key] = ""; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + }, + ); + + it("does not treat an empty QODER_AGENT as an agent", async () => { + process.env["QODER_AGENT"] = ""; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + }); + + it("does not treat Kiro's editor terminal (TERM_PROGRAM=kiro) as an agent", async () => { + process.env["TERM_PROGRAM"] = "kiro"; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + expect(isAgentDriven()).toBe(false); + }); +}); + +describe("detectAgentRuntime: agents seen in discovery hints", () => { + const savedEnv = { ...process.env }; + beforeEach(stripVendorEnv); + afterEach(() => { + process.env = { ...savedEnv }; + }); + + it.each([ + ["WORKBUDDY_CLIENT_USER_AGENT", "wb", "workbuddy"], + ["CODEBUDDY_SESSION_ID", "s", "codebuddy"], + ["CODEBUDDY_AGENTOS_SESSION_ID", "s", "codebuddy"], + ["ICUBE_AI_AGENT_SHUTDOWN_TOKEN", "t", "trae"], + ["TRAE_REMOTE_AGENT_TOKEN", "t", "trae"], + ["AUTO_PIPPIT_THREAD_ID", "t", "pippit"], + ["PIPPIT_OBS_AGENT_NAME", "n", "pippit"], + ["DOUBAO_OFFICE_AGENT_NAME", "n", "doubao"], + ["AILY_CLI_AGENT_DELIVER_BLOCKED", "1", "aily"], + ["GROK_SESSION_ID", "s", "grok"], + ["KIMI_AGENT_CLIENT_ID", "c", "kimi"], + ["ZCODE_SESSION_ID", "s", "zcode"], + ["SWE_AGENT_ENV_FILE", "/tmp/env", "swe_agent"], + ["DSH_SHELL", "1", "dsh"], + ["CLAUDE_AGENT_SDK_VERSION", "0.2.0", "claude_agent_sdk"], + ])("detects %s=%s as %s", async (key, value, runtime) => { + process.env[key] = value; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe(runtime); + expect(isAgentDriven()).toBe(true); + }); + + it("attributes WorkBuddy to workbuddy, not the CodeBuddy keys it also carries", async () => { + process.env["WORKBUDDY_CLIENT_USER_AGENT"] = "wb"; + process.env["CODEBUDDY_SESSION_ID"] = "s"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("workbuddy"); + }); + + it("does not treat a person's dsh terminal (DSH_SESSION_ID only) as an agent", async () => { + process.env["DSH_SESSION_ID"] = "s"; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + expect(isAgentDriven()).toBe(false); + }); + + it.each([ + [{ COPILOT_AGENT: "1", OPENCODE: "1" }, "opencode"], + [{ AI_AGENT: "github_copilot_vscode_agent", QWEN_CODE: "1" }, "qwen_code"], + [{ COPILOT_CLI: "1", GEMINI_CLI: "1" }, "gemini_cli"], + [{ TERM_PROGRAM: "cursor", KILO: "1" }, "kilo"], + [{ TERM_PROGRAM: "windsurf", CRUSH: "1" }, "crush"], + [{ REPL_ID: "r", OPENCODE: "1" }, "opencode"], + ])("lets the agent Copilot or an editor started win: %j -> %s", async (env, runtime) => { + Object.assign(process.env, env); + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe(runtime); + }); + + it("lets Claude Code's own shell marker beat the Agent SDK version key", async () => { + process.env["CLAUDECODE"] = "1"; + process.env["CLAUDE_AGENT_SDK_VERSION"] = "0.2.0"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("claude_code"); + }); + + it("lets a vendor marker beat the Agent SDK version key", async () => { + process.env["CODEBUDDY_SESSION_ID"] = "s"; + process.env["CLAUDE_AGENT_SDK_VERSION"] = "0.2.0"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("codebuddy"); + }); +}); + +describe("detectAgentRuntime: AI_AGENT fallback", () => { + const savedEnv = { ...process.env }; + beforeEach(stripVendorEnv); + afterEach(() => { + process.env = { ...savedEnv }; + }); + + it.each([ + ["claude-code", "claude_code"], + ["claude-code_2-1-307_agent", "claude_code"], + ["opencode", "opencode"], + ["crush", "crush"], + ["pi", "pi"], + ["hermes", "hermes"], + ["hermes-agent", "hermes"], + ["github_copilot_app_agent", "copilot_agent"], + ["antigravity", "antigravity"], + ["trae", "trae"], + ["devin_20261101_agent", "devin"], + ["amp", "amp"], + ["v0", "v0"], + ["rovodev_cli", "rovodev"], + ["base44-superagent", "base44"], + ])("maps the known AI_AGENT name %s to %s", async (value, runtime) => { + process.env["AI_AGENT"] = value; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe(runtime); + }); + + it("classifies an unknown AI_AGENT name as other_ai_agent and as agent-driven", async () => { + process.env["AI_AGENT"] = "some-new-agent"; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("other_ai_agent"); + expect(isAgentDriven()).toBe(true); + }); + + it("lets a vendor marker beat an inherited AI_AGENT value", async () => { + process.env["CLAUDECODE"] = "1"; + process.env["AI_AGENT"] = "opencode"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("claude_code"); + }); + + it.each(["pilot", "crushed", "constructor"])( + "does not map %s to a known runtime by a bare prefix", + async (value) => { + process.env["AI_AGENT"] = value; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("other_ai_agent"); + }, + ); + + it.each(["", " ", "sk-ant-api03", "has space in it"])( + "ignores an empty or unsafe AI_AGENT value %j", + async (value) => { + process.env["AI_AGENT"] = value; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + expect(isAgentDriven()).toBe(false); + }, + ); + + it.each(["1", "true", "some-new-agent"])( + "does not classify an unknown bare AGENT value %j", + async (value) => { + process.env["AGENT"] = value; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBeNull(); + expect(isAgentDriven()).toBe(false); + }, + ); + + it("maps a known name in the bare AGENT variable", async () => { + process.env["AGENT"] = "devin_20261101_agent"; + const { detectAgentRuntime, isAgentDriven } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("devin"); + expect(isAgentDriven()).toBe(true); + }); + + it("prefers a known AGENT name over an unknown AI_AGENT name", async () => { + process.env["AI_AGENT"] = "some-new-agent"; + process.env["AGENT"] = "amp"; + const { detectAgentRuntime } = await import("./agent_runtime.js"); + expect(detectAgentRuntime()).toBe("amp"); + }); +}); + describe("detectAgentHints — new-agent discovery signals", () => { const savedEnv = { ...process.env }; beforeEach(() => { @@ -421,6 +683,13 @@ describe("detectAgentHints — new-agent discovery signals", () => { expect(detectAgentHints().agent_hint).toBe("goose"); }); + it("prefers the AI_AGENT name over a bare AGENT=1 flag", async () => { + process.env["AGENT"] = "1"; + process.env["AI_AGENT"] = "some-new-agent"; + const { detectAgentHints } = await import("./agent_runtime.js"); + expect(detectAgentHints().agent_hint).toBe("some-new-agent"); + }); + it("drops an overlong secret-looking AGENT value rather than leaking it", async () => { process.env["AGENT"] = "sk-ant-api03-THIS-IS-A-LONG-SECRET-LOOKING-VALUE-xyz"; const { detectAgentHints } = await import("./agent_runtime.js"); @@ -492,6 +761,14 @@ describe("detectAgentHints — new-agent discovery signals", () => { expect(detectAgentHints().agent_env_hints).toContain("FOO_AGENT_SESSION_ID"); }); + it("excludes ambient runner, browser and desktop-session keys", async () => { + process.env["AGENT_TOOLSDIRECTORY"] = "/opt/hostedtoolcache"; + process.env["GSM_SKIP_SSH_AGENT_WORKAROUND"] = "true"; + process.env["EDGE_METRICS_SESSION_ID"] = "session"; + const { detectAgentHints } = await import("./agent_runtime.js"); + expect(detectAgentHints().agent_env_hints).toBeNull(); + }); + it("excludes SSH/GPG agent false-friends from agent_env_hints", async () => { process.env["SSH_AGENT_PID"] = "12345"; const { detectAgentHints } = await import("./agent_runtime.js"); @@ -622,6 +899,13 @@ describe("isAgentDriven", () => { [{ TERM_PROGRAM: "windsurf" }, false], [{ REPL_ID: "r" }, false], [{ REPLIT_USER: "u" }, false], + [{ COPILOT_AGENT: "1" }, true], + [{ COPILOT_CLI: "1" }, true], + [{ KILO: "1" }, true], + [{ AGENT_DISPLAY_OUT: "/tmp/d" }, true], + [{ AI_AGENT: "some-new-agent" }, true], + [{ TERM_PROGRAM: "kiro" }, false], + [{ AGENT: "1" }, false], [{}, false], ])("%j -> %s", async (env, expected) => { const { isAgentDriven } = await import("./agent_runtime.js"); diff --git a/packages/cli/src/telemetry/agent_runtime.ts b/packages/cli/src/telemetry/agent_runtime.ts index 24ff50aeed..fd526c50d2 100644 --- a/packages/cli/src/telemetry/agent_runtime.ts +++ b/packages/cli/src/telemetry/agent_runtime.ts @@ -13,7 +13,9 @@ import { detectWSL } from "./platform.js"; // - well-known kernel strings (release(), /proc/version) // - sandbox marker files (/.dockerenv etc.) // - the *existence* of vendor environment variables — never the value -// (some are API keys). +// (some are API keys). The exceptions are self-identifying name or flag +// variables (TERM_PROGRAM, AI_AGENT, `=1` flags), which are only compared +// against fixed values and never emitted raw by detectAgentRuntime(). // // Output is two opaque strings: sandbox_runtime ('gvisor' | 'docker' | ...) // and agent_runtime ('claude_code' | 'codex' | ...). Both null when unknown. @@ -27,6 +29,7 @@ export type AgentRuntime = | "codex" | "cursor" | "copilot_agent" + | "copilot_cli" | "replit" | "hermes" | "openclaw" @@ -36,6 +39,32 @@ export type AgentRuntime = | "cline" | "gemini_cli" | "crush" + | "kilo" + | "opencode" + | "qwen_code" + | "junie" + | "antigravity" + | "qoder" + | "kiro" + | "openhands" + | "workbuddy" + | "codebuddy" + | "trae" + | "pippit" + | "doubao" + | "aily" + | "grok" + | "kimi" + | "zcode" + | "swe_agent" + | "dsh" + | "claude_agent_sdk" + | "devin" + | "amp" + | "v0" + | "rovodev" + | "base44" + | "other_ai_agent" | null; interface VendorRule { @@ -47,8 +76,8 @@ interface VendorRule { } // Ordering matters: the FIRST rule that matches wins. Put more specific rules -// before more generic ones (e.g. copilot_agent before a hypothetical generic -// 'github_actions' rule). +// before more generic ones (e.g. kilo before opencode, whose OPENCODE=1 Kilo's +// bash tool also sets), and the generic AI_AGENT fallback after all of them. const VENDOR_RULES: VendorRule[] = [ // IBM Bob Shell — both shell execution paths stamp BOBSHELL_CLI=1 onto the // child environment: childProcessFallback's spawn env and executeWithPty's @@ -89,46 +118,10 @@ const VENDOR_RULES: VendorRule[] = [ typeof env["CODEX_CI"] === "string" || typeof env["CODEX_SANDBOX_NETWORK_DISABLED"] === "string", }, - // Cursor Agent marks its shell with CURSOR_AGENT; TERM_PROGRAM identifies - // the IDE terminal only. Keep this after Claude/Codex for nested agents. + // Cursor Agent marks its shell with CURSOR_AGENT. Keep this after + // Claude/Codex for nested agents. // Source: https://docs.cursor.com/en/agent/terminal - { - name: "cursor", - check: (env) => Boolean(env["CURSOR_AGENT"]) || env["TERM_PROGRAM"] === "cursor", - agentOnly: (env) => Boolean(env["CURSOR_AGENT"]), - }, - // Windsurf (Codeium) integrated terminal — exports TERM_PROGRAM=windsurf. - // Attested across many independent detectors (nx - // packages/nx/src/native/ide/detection.rs, adonisjs/application, ag-grid - // git-hooks). Compared case-INsensitively (unlike the exact Cursor rule - // above) because Windsurf sources disagree on casing ("windsurf" vs - // "Windsurf"); Cursor's do not, so it stays exact. Like Cursor this marks the - // editor's integrated terminal, not specifically that the Cascade agent is - // driving; under WSL/remote it can also fall back to TERM_PROGRAM=vscode. - { - name: "windsurf", - check: (env) => env["TERM_PROGRAM"]?.toLowerCase() === "windsurf", - agentOnly: () => false, - }, - // GitHub Copilot Coding Agent — runs inside GitHub Actions and the - // workflow injects an additional marker to distinguish from generic CI. - // Not yet verified from a public-source citation in this audit; the var - // names below match GitHub Copilot Coding Agent documentation but - // should be confirmed before relying on attribution. - { - name: "copilot_agent", - check: (env) => - env["GITHUB_ACTIONS"] === "true" && - (typeof env["COPILOT_AGENT_ID"] === "string" || env["RUNNER_NAME"] === "Copilot"), - }, - // Replit — REPL_ID and REPLIT_USER are long-documented environment - // variables exposed inside every Replit workspace. - // Source: https://docs.replit.com/replit-workspace/configuring-the-environment - { - name: "replit", - check: (env) => typeof env["REPL_ID"] === "string" || typeof env["REPLIT_USER"] === "string", - agentOnly: () => false, - }, + { name: "cursor", check: (env) => Boolean(env["CURSOR_AGENT"]) }, // Nous Research Hermes Agent — cli.py:50 unconditionally executes // os.environ["HERMES_QUIET"] = "1" // at module load, so the marker propagates via os.environ to every @@ -194,19 +187,212 @@ const VENDOR_RULES: VendorRule[] = [ name: "crush", check: (env) => typeof env["CRUSH"] === "string", }, + // Kilo Code CLI: bootstrap sets KILO=1 and KILOCODE_VERSION on its own + // process.env, so every shell it spawns inherits both. Must stay ahead of + // opencode: Kilo is an opencode fork and its bash tool also sets OPENCODE=1. + // Source: https://github.com/Kilo-Org/kilocode (packages/opencode/src/kilocode/cli/setup.ts) + { + name: "kilo", + check: (env) => env["KILO"] === "1" || typeof env["KILOCODE_VERSION"] === "string", + }, + // opencode: the bash tool sets OPENCODE=1 and OPENCODE_SESSION_ID (plus the + // generic AGENT=1 and AI_AGENT=opencode) on every command it runs. + // Source: https://github.com/anomalyco/opencode (packages/core/src/tool/plugin/shell.ts) + { + name: "opencode", + check: (env) => env["OPENCODE"] === "1" || typeof env["OPENCODE_SESSION_ID"] === "string", + }, + // Qwen Code: the shell execution service sets QWEN_CODE=1 on the child env + // of both the child_process and PTY spawn paths. + // Source: https://github.com/QwenLM/qwen-code (packages/core/src/services/shellExecutionService.ts) + { + name: "qwen_code", + check: (env) => env["QWEN_CODE"] === "1", + }, + // JetBrains Junie CLI: the launcher shim exports JUNIE_SHIM_PATH before it + // execs the agent, so every process the agent spawns inherits it. + // Source: https://github.com/JetBrains/junie (templates/junie.shim.sh) + { + name: "junie", + check: (env) => typeof env["JUNIE_SHIM_PATH"] === "string", + }, + // Google Antigravity: sets ANTIGRAVITY_AGENT on the commands its agent runs; + // Google's own firebase-tools keys its agent detection on this marker. + // Source: https://github.com/firebase/firebase-tools/pull/10457 (src/env.ts) + { + name: "antigravity", + check: (env) => Boolean(env["ANTIGRAVITY_AGENT"]), + }, + // Qoder: sets a non-empty QODER_AGENT on the terminals its agent runs. + // Source: https://docs.qoder.com/troubleshooting/terminal-execution-exceptions + { + name: "qoder", + check: (env) => Boolean(env["QODER_AGENT"]), + }, + // Kiro: AGENT_DISPLAY_OUT / AGENT_CONTEXT_OUT are "only set when the agent is + // driving". TERM_PROGRAM=kiro is deliberately NOT used: Kiro sets it for a + // person's terminal too. + // Source: https://kiro.dev/docs/reference/built-in-tools + { + name: "kiro", + check: (env) => Boolean(env["AGENT_DISPLAY_OUT"]) || Boolean(env["AGENT_CONTEXT_OUT"]), + }, + // Closed-source agents with no public marker. Each key below shows up in this + // CLI's own agent_env_hints on non-interactive runs, which is direct evidence + // that it is in the env of the shell the agent spawned. Only key presence is + // checked; several values are tokens and are never read. WorkBuddy is built + // on CodeBuddy and carries its keys too, so it goes first. + // Source: agent_env_hints discovery telemetry (detectAgentHints below). + { name: "workbuddy", check: (env) => typeof env["WORKBUDDY_CLIENT_USER_AGENT"] === "string" }, + { + name: "codebuddy", + check: (env) => + typeof env["CODEBUDDY_SESSION_ID"] === "string" || + typeof env["CODEBUDDY_AGENTOS_SESSION_ID"] === "string", + }, + { + name: "trae", + check: (env) => + typeof env["ICUBE_AI_AGENT_SHUTDOWN_TOKEN"] === "string" || + typeof env["TRAE_REMOTE_AGENT_TOKEN"] === "string", + }, + { + name: "pippit", + check: (env) => + typeof env["AUTO_PIPPIT_THREAD_ID"] === "string" || + typeof env["PIPPIT_OBS_AGENT_NAME"] === "string", + }, + { name: "doubao", check: (env) => typeof env["DOUBAO_OFFICE_AGENT_NAME"] === "string" }, + { name: "aily", check: (env) => typeof env["AILY_CLI_AGENT_DELIVER_BLOCKED"] === "string" }, + { name: "grok", check: (env) => typeof env["GROK_SESSION_ID"] === "string" }, + { name: "kimi", check: (env) => typeof env["KIMI_AGENT_CLIENT_ID"] === "string" }, + { name: "zcode", check: (env) => typeof env["ZCODE_SESSION_ID"] === "string" }, + { name: "swe_agent", check: (env) => typeof env["SWE_AGENT_ENV_FILE"] === "string" }, + // DeepSeek harness (dsh): every model shell call, bash or pwsh, runs with + // DSH_SHELL=1. DSH_SESSION_ID is NOT used: dsh also sets it on the interactive + // terminal a person types into. HyperFrames Desktop runs dsh too; those runs + // also carry HYPERFRAMES_CLIENT, which tells the two apart. + // Source: https://github.com/deepseek-ai/deepseek-harness (packages/terminal/terminal-bash/src/index.ts, + // packages/api/terminal-controller/src/index.ts) + { name: "dsh", check: (env) => env["DSH_SHELL"] === "1" }, + // GitHub Copilot CLI: shell commands and MCP servers receive + // COPILOT_AGENT_SESSION_ID, and its subprocesses carry COPILOT_CLI=1. + // Both Copilot rules sit after the other agents: their markers are inherited, + // so an agent Copilot starts must win. copilot_cli goes first because the CLI + // run from a VS Code agent terminal inherits that terminal's COPILOT_AGENT=1. + // Source: https://github.com/github/copilot-cli (changelog.md) + { + name: "copilot_cli", + check: (env) => + env["COPILOT_CLI"] === "1" || typeof env["COPILOT_AGENT_SESSION_ID"] === "string", + }, + // GitHub Copilot agent mode in VS Code: the agent's terminal tool sets + // AI_AGENT=github_copilot_ and COPILOT_AGENT=1 on agent terminals + // only, never on a person's terminal. + // Source: https://github.com/microsoft/vscode (src/vs/platform/chat/common/aiAgentEnv.ts, + // src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/toolTerminalCreator.ts) + { + name: "copilot_agent", + check: (env) => + env["COPILOT_AGENT"] === "1" || env["AI_AGENT"]?.startsWith("github_copilot_") === true, + }, + // OpenHands: sanitized_env() defaults AI_AGENT=openhands on every command + // the agent runs (it keeps an inherited value, so an outer agent's name wins). + // Last of the vendor rules because AI_AGENT is the generic convention. + // Source: https://github.com/OpenHands/software-agent-sdk (openhands-sdk/openhands/sdk/utils/command.py) + { + name: "openhands", + check: (env) => env["AI_AGENT"]?.trim().toLowerCase() === "openhands", + }, + // An app built on the Claude Agent SDK, running a command outside Claude + // Code's own shell tool (that path sets CLAUDECODE, caught above). The SDK + // always sets CLAUDE_AGENT_SDK_VERSION on the process it spawns. After the + // agent rules, so a vendor that builds on the SDK and sets its own marker wins. + // Source: https://github.com/anthropics/claude-agent-sdk-python (src/claude_agent_sdk/_internal/transport/subprocess_cli.py) + { + name: "claude_agent_sdk", + check: (env) => typeof env["CLAUDE_AGENT_SDK_VERSION"] === "string", + }, + // Editor terminals and hosted workspaces: these mark where the shell runs, + // not that an agent drives it, so every agent marker above wins over them. + // Cursor's TERM_PROGRAM identifies the IDE terminal only. + // Source: https://docs.cursor.com/en/agent/terminal + { name: "cursor", check: (env) => env["TERM_PROGRAM"] === "cursor", agentOnly: () => false }, + // Windsurf (Codeium) integrated terminal — exports TERM_PROGRAM=windsurf. + // Attested across many independent detectors (nx + // packages/nx/src/native/ide/detection.rs, adonisjs/application, ag-grid + // git-hooks). Compared case-INsensitively (unlike the exact Cursor rule + // above) because Windsurf sources disagree on casing ("windsurf" vs + // "Windsurf"); Cursor's do not, so it stays exact. Like Cursor this marks the + // editor's integrated terminal, not specifically that the Cascade agent is + // driving; under WSL/remote it can also fall back to TERM_PROGRAM=vscode. + { + name: "windsurf", + check: (env) => env["TERM_PROGRAM"]?.toLowerCase() === "windsurf", + agentOnly: () => false, + }, + // Replit — REPL_ID and REPLIT_USER are long-documented environment + // variables exposed inside every Replit workspace. + // Source: https://docs.replit.com/replit-workspace/configuring-the-environment + { + name: "replit", + check: (env) => typeof env["REPL_ID"] === "string" || typeof env["REPLIT_USER"] === "string", + agentOnly: () => false, + }, ]; +// AI_AGENT convention: an agent sets AI_AGENT= and keeps an inherited +// value, so the outermost agent's name wins. Checked AFTER every VENDOR_RULE so +// the immediate executor's own marker takes precedence. Unknown slug-safe names +// become "other_ai_agent" (the raw name goes out as agent_hint). Bare AGENT only +// counts when it holds a known name: it has no agreed values and AGENT=1 is +// common. A known name matches exactly or as a `_` / `-` prefix +// (Claude Code sends claude-code_2-1-307_agent, Devin devin__agent); the +// boundary keeps "pi" from swallowing "pilot". Names past hermes come from the +// values this CLI's agent_hint reports. +// Sources: https://github.com/vercel/vercel (packages/detect-agent), +// https://github.com/agentsmd/agents.md/pull/136 +const AI_AGENT_RUNTIMES: ReadonlyArray]> = [ + ["claude-code", "claude_code"], + ["opencode", "opencode"], + ["crush", "crush"], + ["pi", "pi"], + ["hermes", "hermes"], + ["github_copilot", "copilot_agent"], + ["openhands", "openhands"], + ["antigravity", "antigravity"], + ["trae", "trae"], + ["devin", "devin"], + ["amp", "amp"], + ["v0", "v0"], + ["rovodev", "rovodev"], + ["base44", "base44"], +]; + +function knownAgentName(value: string | null): AgentRuntime { + if (value === null) return null; + const known = AI_AGENT_RUNTIMES.find( + ([name]) => value === name || value.startsWith(`${name}_`) || value.startsWith(`${name}-`), + ); + return known?.[1] ?? null; +} + +function aiAgentFallback(env: NodeJS.ProcessEnv): AgentRuntime { + const aiAgent = sanitizeHint(env["AI_AGENT"]); + return ( + knownAgentName(aiAgent) ?? + knownAgentName(sanitizeHint(env["AGENT"])) ?? + (aiAgent === null ? null : "other_ai_agent") + ); +} + // Agents evaluated and deliberately NOT added. Each fails the bar the rules // above meet — a marker reliably present in the environment of the // shell/subprocess the agent spawns. Recorded here (not only in the PR) so the // next person doesn't re-derive it: -// - OpenHands — OPENHANDS_BUILD_GIT_SHA/_REF exists in the agent-server -// Dockerfile (base-image-minimal stage, added 2025-11-09 in PR #1100) but -// is empirically ABSENT from the runtime env of every published -// ghcr.io/openhands/agent-server image inspected (12+ tags, 2025-10 → -// 2026-01, incl. the introducing PR's merge commit). The declared ENV -// never reaches the published image. Re-add only if a real published image -// carries it in `docker inspect .Config.Env`. +// - OpenHands OPENHANDS_BUILD_GIT_SHA/_REF: declared in the agent-server +// Dockerfile but absent from every published agent-server image inspected. +// OpenHands is detected through AI_AGENT=openhands instead (rule above). // - Aider — sets no self-identifying env var; both shell-spawn sites // (run_cmd.py Popen / pexpect.spawn) pass no env=, so children inherit // os.environ verbatim. @@ -214,14 +400,22 @@ const VENDOR_RULES: VendorRule[] = [ // and the computercontroller MCP extension, but NOT on the default // developer `shell` tool (sets only PATH+cwd), so the primary path is // undetected. -// - opencode — OPENCODE_TERMINAL=1 is set only on the interactive PTY panel, -// not on the model's bash/shell tool. +// - opencode OPENCODE_TERMINAL: PTY panel only, not the bash tool (opencode +// is detected through the bash tool's own markers, rule above). // - Roo Code — ROO_ACTIVE is set only on the `vscode` terminal provider; the // shipped default is the execa provider (terminalShellIntegrationDisabled // defaults true), which sets no marker. -// - Amp / Devin / Jules / Factory Droid — no verifiable unconditional runtime -// marker (Amp is closed/minified; Devin/Jules are closed sandboxes; -// Factory's FACTORY_PROJECT_DIR / DROID_PLUGIN_ROOT are hook-scoped only). +// - Jules / Factory Droid — no verifiable unconditional runtime marker +// (Jules is a closed sandbox; Factory's FACTORY_PROJECT_DIR / +// DROID_PLUGIN_ROOT are hook-scoped only). Amp and Devin are detected by +// the agent names they report instead (AI_AGENT_RUNTIMES). +// - Seen only in a shipped bundle: Grok GROK_AGENT (also conflicts with an +// older probe; GROK_SESSION_ID is used), Augment AUGMENT_AGENT, Factory +// FACTORY_SESSION_ID. +// - Third-party detector packages only: Trae TRAE_AI_SHELL_ID, Windsurf +// WINDSURF_CASCADE_TERMINAL (plus a forum post). +// - Kimi Code: its bash tool sets no marker (MoonshotAI/kimi-code bashTool.ts); +// the kimi rule keys on the hosted agent's KIMI_AGENT_CLIENT_ID. /** * Identify the managed sandbox runtime hosting this CLI invocation. @@ -255,13 +449,16 @@ export function detectAgentRuntime(): AgentRuntime { for (const rule of VENDOR_RULES) { if (rule.check(process.env)) return rule.name; } - return null; + return aiAgentFallback(process.env); } /** An agent is running this shell, not a person typing in an editor's terminal or a hosted workspace. */ export function isAgentDriven(env: NodeJS.ProcessEnv = process.env): boolean { if (isGeminiManagedAgent()) return true; - return VENDOR_RULES.some((rule) => (rule.agentOnly ?? rule.check)(env)); + return ( + VENDOR_RULES.some((rule) => (rule.agentOnly ?? rule.check)(env)) || + aiAgentFallback(env) !== null + ); } /** Observed harness marker, not proof of a vendor, benchmark, or human user. */ @@ -282,7 +479,8 @@ export function detectExecutionHarnessHint(): "harbor" | null { // detectAgentHints() adds a self-populating residual signal for exactly that // null bucket, so an unrecognized agent surfaces on its own in analytics and // can be promoted to a real VENDOR_RULE later. Callers should only emit these -// when detectAgentRuntime() returns null — a classified event needs no hint. +// when detectAgentRuntime() returns null or "other_ai_agent" (the AI_AGENT +// value no rule names); any other classified event needs no hint. // // Privacy — consistent with the "never read secret-shaped values" stance above: // - agent_env_hints emits KEY NAMES only, never values. @@ -296,8 +494,9 @@ export function detectExecutionHarnessHint(): "harbor" | null { export interface AgentHints { /** - * Best-effort agent name from a self-identifying env-var value: AGENT, else - * AI_AGENT. null when neither is set or the value isn't a short safe slug. + * Best-effort agent name: AI_AGENT (what "other_ai_agent" was classified + * from), else AGENT, which is often just a flag like AGENT=1. null when neither + * is set or the value isn't a short safe slug. */ agent_hint: string | null; /** @@ -362,8 +561,10 @@ function sanitizeHint(value: string | undefined): string | null { } // A key (uppercased) looks like a coding-agent marker. Excludes the two -// value-captured generics (read via agent_hint) and the SSH/GPG "agent" false -// friends, which are credential agents, not coding agents. No bare `CODING` +// value-captured generics (read via agent_hint), the SSH/GPG "agent" false +// friends, which are credential agents, not coding agents, and a few ambient +// keys that only match by accident (a CI runner's toolcache path, a browser's +// metrics session, a desktop session's ssh-agent switch). No bare `CODING` // token — it substring-matches `ENCODING` (e.g. PYTHONIOENCODING) and real // coding-agent keys already match via `AGENT` (e.g. PI_CODING_AGENT). export const HINT_KEY_PATTERN = /AGENT|ASSISTANT|COPILOT|CODEX|CLAUDE|LLM|_THREAD_ID$|_SESSION_ID$/; @@ -371,7 +572,16 @@ export const HINT_KEY_PATTERN = /AGENT|ASSISTANT|COPILOT|CODEX|CLAUDE|LLM|_THREA function isDiscoveryHintKey(upperKey: string): boolean { if (upperKey === "AGENT" || upperKey === "AI_AGENT") return false; if (upperKey.startsWith("SSH_") || upperKey.startsWith("GPG_")) return false; - if (["NPM_CONFIG_USER_AGENT", "TERM_SESSION_ID", "XDG_SESSION_ID"].includes(upperKey)) + if ( + [ + "NPM_CONFIG_USER_AGENT", + "TERM_SESSION_ID", + "XDG_SESSION_ID", + "AGENT_TOOLSDIRECTORY", + "GSM_SKIP_SSH_AGENT_WORKAROUND", + "EDGE_METRICS_SESSION_ID", + ].includes(upperKey) + ) return false; return HINT_KEY_PATTERN.test(upperKey); } @@ -390,7 +600,7 @@ function discoveryHintPriority(key: string): number { */ export function detectAgentHints(): AgentHints { const env = process.env; - const agent_hint = sanitizeHint(env["AGENT"]) ?? sanitizeHint(env["AI_AGENT"]); + const agent_hint = sanitizeHint(env["AI_AGENT"]) ?? sanitizeHint(env["AGENT"]); const term_program = sanitizeHint(env["TERM_PROGRAM"]); const keys = new Set(); diff --git a/packages/cli/src/telemetry/system.test.ts b/packages/cli/src/telemetry/system.test.ts index 79d0ebd39b..2d0718e594 100644 --- a/packages/cli/src/telemetry/system.test.ts +++ b/packages/cli/src/telemetry/system.test.ts @@ -26,6 +26,53 @@ describe("getSystemMeta execution context", () => { }); }); +describe("getSystemMeta CI provider", () => { + const CI_KEYS = [ + "GITHUB_ACTIONS", + "GITLAB_CI", + "CIRCLECI", + "JENKINS_URL", + "BUILDKITE", + "TRAVIS", + "CODEBUILD_BUILD_ID", + "CONTINUOUS_INTEGRATION", + "CI", + ]; + + it("names AWS CodeBuild from CODEBUILD_BUILD_ID", async () => { + const savedEnv = { ...process.env }; + try { + for (const key of CI_KEYS) delete process.env[key]; + process.env["CODEBUILD_BUILD_ID"] = "project:0123"; + const { getSystemMeta } = await import("./system.js"); + const meta = getSystemMeta(); + expect(meta.is_ci).toBe(true); + expect(meta.ci_name).toBe("codebuild"); + } finally { + process.env = savedEnv; + } + }); +}); + +describe("getSystemMeta agent hints", () => { + it("keeps the raw AI_AGENT name in agent_hint when it classifies as other_ai_agent", async () => { + const savedEnv = { ...process.env }; + try { + for (const key of Object.keys(process.env)) { + if (/AGENT|CLAUDE|CODEX|COPILOT|CURSOR|TERM_PROGRAM/.test(key)) delete process.env[key]; + } + process.env["AGENT"] = "1"; + process.env["AI_AGENT"] = "some-new-agent"; + const { getSystemMeta } = await import("./system.js"); + const meta = getSystemMeta(); + expect(meta.agent_runtime).toBe("other_ai_agent"); + expect(meta.agent_hint).toBe("some-new-agent"); + } finally { + process.env = savedEnv; + } + }); +}); + describe("getSystemMeta client", () => { afterEach(() => vi.unstubAllEnvs()); diff --git a/packages/cli/src/telemetry/system.ts b/packages/cli/src/telemetry/system.ts index 2e3be2718d..7c03326658 100644 --- a/packages/cli/src/telemetry/system.ts +++ b/packages/cli/src/telemetry/system.ts @@ -58,7 +58,7 @@ export interface SystemMeta { /** * New-agent discovery signals for the agent_runtime=null bucket, so an agent * we have no rule for surfaces on its own instead of vanishing into null. - * All three are null on a classified event (agent_runtime != null) and on a + * All three are null on a classified event (except "other_ai_agent") and on a * plain shell with no markers. See `detectAgentHints` in agent_runtime.ts for * the fields and the privacy contract. */ @@ -93,7 +93,7 @@ export function getSystemMeta(): SystemMeta { // needs no hint, and gating keeps them off the ~80%+ of classified events. const agent_runtime = detectAgentRuntime(); const hints = - agent_runtime === null + agent_runtime === null || agent_runtime === "other_ai_agent" ? detectAgentHints() : { agent_hint: null, term_program: null, agent_env_hints: null }; @@ -150,6 +150,7 @@ const CI_PROVIDERS: CIProvider[] = [ { name: "jenkins", envVar: "JENKINS_URL", mode: "presence" }, { name: "buildkite", envVar: "BUILDKITE", mode: "truthy" }, { name: "travis", envVar: "TRAVIS", mode: "truthy" }, + { name: "codebuild", envVar: "CODEBUILD_BUILD_ID", mode: "presence" }, { name: null, envVar: "CONTINUOUS_INTEGRATION", mode: "truthy" }, { name: null, envVar: "CI", mode: "truthy" }, ]; diff --git a/packages/cli/src/utils/attendedTerminal.test.ts b/packages/cli/src/utils/attendedTerminal.test.ts index 9da0c664d7..084d971472 100644 --- a/packages/cli/src/utils/attendedTerminal.test.ts +++ b/packages/cli/src/utils/attendedTerminal.test.ts @@ -78,7 +78,7 @@ describe("isUnattended", () => { async (name, value) => { for (const key of Object.keys(process.env)) { if ( - /^(CLAUDE|CODEX_|CURSOR_|BOBSHELL|GEMINI_CLI|CLINE_|HERMES_|PI_CODING|CRUSH|OPENCLAW)/.test( + /^(CLAUDE|CODEX_|CURSOR_|BOBSHELL|GEMINI_CLI|CLINE_|HERMES_|PI_CODING|CRUSH|OPENCLAW|AI_AGENT$|AGENT$|WORKBUDDY_|CODEBUDDY_|ICUBE_|TRAE_|AUTO_PIPPIT|PIPPIT_|DOUBAO_|AILY_|GROK_|KIMI_AGENT|ZCODE_|SWE_AGENT|DSH_|COPILOT_|KILO|OPENCODE|QWEN_CODE|JUNIE_|ANTIGRAVITY_|QODER_|AGENT_(DISPLAY|CONTEXT)_OUT)/.test( key, ) ) {