Skip to content

PYTHON-5929 Add coding agent env var to handshake metadata#2945

Draft
aclark4life wants to merge 5 commits into
mongodb:mainfrom
aclark4life:PYTHON-5929
Draft

PYTHON-5929 Add coding agent env var to handshake metadata#2945
aclark4life wants to merge 5 commits into
mongodb:mainfrom
aclark4life:PYTHON-5929

Conversation

@aclark4life

@aclark4life aclark4life commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5929

DRIVERS-3529

Changes in this PR

Report coding-agent environment variables in the client handshake metadata so that agentic usage of MongoDB/Atlas can be quantified.

  • Added _metadata_agent() and the ordered _AGENT_ENV_VARS list in pymongo/pool_options.py with detection precedence:
    1. AI_AGENT, then AGENT
    2. First matching known agent variable maps to fixed value: CLAUDECODECLAUDECODE, CURSOR_AGENTCURSOR, GEMINI_CLIGEMINI_CLI, CODEX_SANDBOXCODEX_SANDBOX, AUGMENT_AGENTAUGMENT, OPENCODE_CLIENTOPENCODE.
  • _metadata_env() now sets client.env.agent when detected and coexists with env.name (the serverless / Function-as-a-Service provider, e.g. AWS Lambda) and env.container.
  • _truncate_metadata() preserves both env.name and env.agent in its first truncation step

Note

The handshake spec is not yet updated for this field. Field name/values follow the DRIVERS-3529 Jira design and may need to be reconciled with the final spec and the Node driver implementation.

Test Plan

Added tests in test/asynchronous/test_client.py covering known-agent detection, generic AI_AGENT/AGENT precedence, and agent reported alongside a serverless provider (e.g. AWS Lambda).

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s). (Spec finalization tracked under DRIVERS-3529.)

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Detect coding agent environment variables (AI_AGENT, AGENT, CLAUDECODE,
CURSOR_AGENT, GEMINI_CLI, CODEX_SANDBOX, AUGMENT_AGENT, OPENCODE_CLIENT)
and report them in the client.env.agent handshake field. See DRIVERS-3529.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds detection of “coding agent” environment variables and reports them in the MongoDB client handshake metadata (client.env.agent) so agentic usage can be measured, alongside existing env.name (FaaS provider) and env.container fields.

Changes:

  • Add _metadata_agent() plus _AGENT_ENV_VARS precedence mapping, and populate env["agent"] in _metadata_env().
  • Update _truncate_metadata() to retain env.agent in the first truncation step (alongside env.name).
  • Add async + sync handshake tests covering known-agent mapping, generic AI_AGENT/AGENT precedence, and coexistence with a FaaS provider.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pymongo/pool_options.py Implements agent env-var detection, adds env.agent to handshake metadata, and updates metadata truncation behavior.
test/asynchronous/test_client.py Adds handshake tests for agent detection and interaction with FaaS provider metadata.
test/test_client.py Sync-mirrored handshake tests corresponding to the async additions.

Comment thread pymongo/pool_options.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread pymongo/pool_options.py
Comment on lines +239 to +243
# 1. Omit fields from env except env.name and env.agent.
env = metadata.get("env", {})
trimmed_env = {k: env[k] for k in ("name", "agent") if k in env}
if trimmed_env:
metadata["env"] = trimmed_env
Comment on lines +2219 to +2224
async def test_handshake_12_agent_with_provider(self):
# agent is reported alongside a FaaS provider.
await self._test_handshake(
{"FUNCTIONS_WORKER_RUNTIME": "python", "CLAUDECODE": "1"},
{"name": "azure.func", "agent": "CLAUDECODE"},
)
Comment on lines +2207 to +2211
async def test_handshake_10_agent_known(self):
# A known coding-agent env var maps to its metadata value.
await self._test_handshake({"CLAUDECODE": "1"}, {"agent": "CLAUDECODE"})
await self._test_handshake({"CURSOR_AGENT": "1"}, {"agent": "CURSOR"})
await self._test_handshake({"OPENCODE_CLIENT": "1"}, {"agent": "OPENCODE"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants