Repository navigation
Conversation
50be426 to
cb1f4c9
Compare
612b981 to
b9f4582
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The tests retain host Kubernetes state that causes failures on Kubernetes runners, and the application-name assertions no longer verify output correctness for the multibyte truncation fix.
2 open issues
What changed in this PR
Adds AI-agent identification to the MongoDB driver’s handshake metadata and refactors environment metadata handling. Full alignment with the specification’s truncation order remains deferred to NODE-7851.
Changes:
- Detects and normalizes agent environment variables using ordered precedence.
- Refactors environment construction and size accounting to support field replacement.
- Adds UTF-8-safe byte truncation for agent identifiers and application names, with unit and integration coverage.
| File | Description |
|---|---|
test/unit/utils.test.ts |
Tests byte-limited string truncation and invalid limits. |
test/unit/cmap/handshake/client_metadata.test.ts |
Updates metadata sizing, truncation, and agent-detection tests. |
test/mongodb_bundled.ts |
Exposes new helpers to bundled tests. |
test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts |
Adds agent handshake cases and environment-isolation helpers. |
src/utils.ts |
Adds UTF-8-safe byte truncation. |
src/sdam/monitor.ts |
Adapts FaaS detection to the empty-map return value. |
src/cmap/handshake/client_metadata.ts |
Adds agent metadata and refactors environment construction and sizing. |
Review effort: Balanced
| // the above assertion fails if `metadata.application?.name` is undefined, so | ||
| // we can safely assert that it exists | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(129); |
There was a problem hiding this comment.
After restoring this test, we can flip it to check for 126, thus ensuring that the truncation worked.
There was a problem hiding this comment.
Changes look good, no blockers, approving.
Will merge once Team Review is done, we have release notes, and NODE-7851 is ready to be merged.
| // Env var value length after truncating bytes | ||
| const APP_NAME_TRUNCATION_LIMIT_BYTES = 128; |
There was a problem hiding this comment.
| // Env var value length after truncating bytes | |
| const APP_NAME_TRUNCATION_LIMIT_BYTES = 128; | |
| // Maximum size of application.name in bytes | |
| const APP_NAME_TRUNCATION_LIMIT_BYTES = 128; |
This limit (value length) applies to appName, not to an environment variable value.
| // Env var value length after first 2 forms of normalization | ||
| const AGENT_ENV_LIMIT_BYTES = 64; |
There was a problem hiding this comment.
| // Env var value length after first 2 forms of normalization | |
| const AGENT_ENV_LIMIT_BYTES = 64; | |
| // Maximum size of env.agent in bytes, applied after trim and lowercase | |
| const AGENT_ENV_LIMIT_BYTES = 64; |
Just a little bit more direct


Description
Summary of Changes
env.agent, to the client metadata handshake.envsub-document is built and truncated.Notes for Reviewers
env.agentenv.agentis populated using first-match, order-bound logic over the agent-related environment variables defined in the spec. Depending on which variable matches, the value is either a generic marker (AI_AGENT) or the variable's literal value.Per the spec, "a variable is considered populated if it is present in the environment with a non-empty value." We treat any value that is empty after normalization/truncation as unpopulated and skip it.
envconstruction and truncationThe incoming spec's truncation order is:
Note: The truncation order compliance is to be done in NODE-7851 as we're currently not compliant with even the previous version of the spec.
envis built by progressively appending members to the live metadata document, and the spec was partially-satisfied somewhat incidentally:env.namehappened to be the last FaaS member added, so it was deleted last — butenv.containeris appended after that, making it theoretically possible to end up with anenvcontainingcontainerbut noname. This is incorrect.envis now constructed as a standalone document, truncated independently. This prepares us for a future spec alignment effort tracked in NODE-7851.Truncation is a byte-op. This means that we can potentially slice multi-byte values during truncation. The remnants of a multi-byte slice are discarded. This allows us to also close out an outstanding bug (NODE-5150) via the use of a shared util (DriverStringUtils.truncateStringBytes) where multi-byte partials remained when constructing appName.
Other info
Refer to: mongodb/specifications#1967 for the incoming changes.
What is the motivation for this change?
For bug fixes
Current (incorrect) behavior:
Expected behavior:
How to reproduce:
Affected versions:
Release Highlight
AI agent identifier now in client metadata
The Node.js driver now includes additional runtime environment details, including AI agent information, when connecting to MongoDB. This provides better visibility into the applications and environments using the driver.
Double check the following
npm run check:lint)type(NODE-xxxx)[!]: descriptionfeat(NODE-1234)!: rewriting everything in coffeescript