PYTHON-5929 Add coding agent env var to handshake metadata#2945
Draft
aclark4life wants to merge 5 commits into
Draft
PYTHON-5929 Add coding agent env var to handshake metadata#2945aclark4life wants to merge 5 commits into
aclark4life wants to merge 5 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
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_VARSprecedence mapping, and populateenv["agent"]in_metadata_env(). - Update
_truncate_metadata()to retainenv.agentin the first truncation step (alongsideenv.name). - Add async + sync handshake tests covering known-agent mapping, generic
AI_AGENT/AGENTprecedence, 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 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"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
_metadata_agent()and the ordered_AGENT_ENV_VARSlist inpymongo/pool_options.pywith detection precedence:AI_AGENT, thenAGENTCLAUDECODE→CLAUDECODE,CURSOR_AGENT→CURSOR,GEMINI_CLI→GEMINI_CLI,CODEX_SANDBOX→CODEX_SANDBOX,AUGMENT_AGENT→AUGMENT,OPENCODE_CLIENT→OPENCODE._metadata_env()now setsclient.env.agentwhen detected and coexists withenv.name(the serverless / Function-as-a-Service provider, e.g. AWS Lambda) andenv.container._truncate_metadata()preserves bothenv.nameandenv.agentin its first truncation stepNote
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.pycovering known-agent detection, genericAI_AGENT/AGENTprecedence, and agent reported alongside a serverless provider (e.g. AWS Lambda).Checklist
Checklist for Author
Checklist for Reviewer