Skip to content

Detect opencode as an AI agent in GetAIAgent() #664

Description

@WilliamBergamin

Problem

opencode (the open-source AI coding agent) sets two environment variables unconditionally at startup for every session:

  • process.env.AGENT = "1"
  • process.env.OPENCODE = "1"

Source (opencode, pinned to commit 3f39a329c3d52ed66405c4bc6293b9ed08fe9ab6):
https://github.com/anomalyco/opencode/blob/3f39a329c3d52ed66405c4bc6293b9ed08fe9ab6/packages/opencode/src/index.ts#L75-L76

Because AGENT is set to the literal "1", the generic fallback in GetAIAgent() catches it and records the agent name verbatim as "1". As a result, opencode-driven CLI runs surface in the User-Agent header, the ai_agent telemetry field, and the Jaeger ai_agent trace tag as:

slack-cli/<version> (os: darwin) AI-Agent (name: 1)

So the run is detectable as an AI agent but mislabeled as 1 instead of opencode, which is noisy for agent attribution / dashboards.

Note on sourcing: opencode's published docs (opencode.ai/docs) do not document these env vars; the authoritative source is the repo source code linked above.

Proposed change

Add a dedicated OPENCODE case to GetAIAgent() in internal/useragent/useragent.go, matching the existing hardcoded agents (claude-code, codex, gemini-cli, cline, cursor). It must be placed before the generic AGENT fallback so opencode wins over the bare AGENT=1:

case os.Getenv("CURSOR_AGENT") == "1":
    return &AIAgent{Name: "cursor"}
case os.Getenv("OPENCODE") == "1":
    return &AIAgent{Name: "opencode"}
case os.Getenv("AGENT") != "":
    return &AIAgent{Name: os.Getenv("AGENT")}

The GetAIAgent doc-comment priority list should also be updated to include OPENCODE (e.g. ... > CURSOR_AGENT > OPENCODE > AGENT).

Offer

Happy to send a PR with this change plus a table-driven test case in useragent_test.go, pending maintainer feedback per CONTRIBUTING.md.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementM-T: A feature request for new functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions