Repository navigation
feat: mark extension and worker user agents (#985) - #992
srivilliamsai wants to merge 5 commits into
Conversation
Add tests checking user_agent_extra on every default boto3 client in extensions and compute-worker and verifying caller-provided clients keep their own user agent.
Set user_agent_extra with package name, version, and feature marker (md/microvm, md/agentcore, or md/ai) on every default boto3 client created by extensions and compute-worker. Document that caller-provided clients are used as they are and keep their own user agent.
| user_agent_extra=( | ||
| "aws-durable-execution-sdk-python-compute-worker/" | ||
| f"{__version__} md/microvm" | ||
| ), |
There was a problem hiding this comment.
Should fix: AgentCore worker calls get the MicroVM label.
The AgentCore worker sends its callback calls with the label md/microvm instead of md/agentcore. So service logs will count AgentCore traffic as MicroVM traffic.
Why: when the caller gives no create_client (a function that makes the Lambda client), the AgentCore worker calls CallbackReporter.create(...) from the MicroVM module (agentcore/worker.py L341-L342). That builds its client with _create_client, which always adds md/microvm (this line). The extensions package labels its own AgentCore client md/agentcore (compute/agentcore/operation.py L732-L735), so the two packages disagree.
What to change: let CallbackReporter.create and _create_client take the feature label, and have the AgentCore worker pass "agentcore". A label check in test_creates_its_own_reporter_without_create_client would catch this.
🤖 AI-assisted comment
There was a problem hiding this comment.
@SilanHe Fixed in 62b71938: _create_client and CallbackReporter.create now accept *, feature: str = "microvm", agentcore/worker.py passes feature="agentcore", and test_creates_its_own_reporter_without_create_client + test_default_client_sets_user_agent verify the md/agentcore marker.
| def test_default_client_sets_user_agent(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("AWS_DEFAULT_REGION", "us-east-1") | ||
| original: Any = boto3.client | ||
| created: list[Any] = [] | ||
| stubbers: list[Stubber] = [] | ||
|
|
||
| def spy_client(service: str, **kwargs: Any) -> Any: | ||
| client = original(service, **kwargs) | ||
| stubber = Stubber(client) | ||
| stubber.add_response("converse", reply("ok")) | ||
| stubber.activate() | ||
| created.append(client) | ||
| stubbers.append(stubber) | ||
| return client | ||
|
|
||
| monkeypatch.setattr(boto3, "client", spy_client) | ||
| model = llm() | ||
| try: | ||
| assert model._converse(model._config, "Question: Q?") == "ok" | ||
| [client] = created | ||
| assert client.meta.config.user_agent_extra == ( | ||
| f"aws-durable-execution-sdk-python-extensions/{__version__} md/ai" | ||
| ) | ||
| finally: | ||
| for stubber in stubbers: | ||
| stubber.deactivate() | ||
| for client in created: | ||
| client.close() | ||
|
|
||
|
|
||
| def test_passed_client_keeps_its_user_agent(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| def fail_boto_client(*args: Any, **kwargs: Any) -> Any: | ||
| msg = "boto3.client must not be called when client is passed" | ||
| raise AssertionError(msg) | ||
|
|
||
| monkeypatch.setattr(boto3, "client", fail_boto_client) | ||
| custom: Any = boto3.session.Session().client( | ||
| "bedrock-runtime", | ||
| region_name="us-east-1", | ||
| config=Config(user_agent_extra="custom-agent/1.0"), | ||
| ) | ||
| model = llm(BedrockConfig(client=custom)) | ||
| try: | ||
| with Stubber(custom) as stubber: | ||
| stubber.add_response("converse", reply("ok")) | ||
| assert model._converse(model._config, "Question: Q?") == "ok" |
There was a problem hiding this comment.
Should fix: these two new tests use code that main has removed.
Both tests call model._converse(model._config, ...) (L270, L297). The second also builds llm(BedrockConfig(client=custom)). On main, llm() now takes a transport as its first argument, and Llm has no _converse or _config any more (main ai/llm.py L317). So both tests fail once this branch is merged with main.
What to change: rewrite them on top of bedrock_transport(BedrockConfig(...)), the way main's test_matches_the_boto3_request_shape does now.
🤖 AI-assisted comment
There was a problem hiding this comment.
@SilanHe Fixed in 54d1f7f0 / 62b71938: merged upstream/main and rewrote test_default_client_sets_user_agent on top of bedrock_transport().
| def test_passed_client_keeps_its_user_agent() -> None: | ||
| custom: Any = boto3.session.Session().client( | ||
| "lambda", | ||
| region_name="us-east-1", | ||
| config=Config(user_agent_extra="custom-agent/1.0"), | ||
| ) | ||
| try: | ||
| reporter = CallbackReporter("cb-1", custom) | ||
| assert reporter._client is custom # noqa: SLF001 | ||
| client_config: Any = custom.meta.config | ||
| assert client_config.user_agent_extra == "custom-agent/1.0" |
There was a problem hiding this comment.
Should fix: this check would pass even if the code were broken.
The test makes a client with the label "custom-agent/1.0" and then checks that the client still has that label. The code never touches a client you pass in, so this line can't fail. The line that matters is the is custom check above it. That check repeats test_the_given_client_is_used_and_no_client_is_created, which already proves that a passed client is used as it is.
The same pattern is in the "passed client" tests in agentcore_operation_test.py, validation_test.py, transports_test.py and llm_test.py.
What to change: drop these tests, or keep only the identity check and fold it into the existing test.
🤖 AI-assisted comment
There was a problem hiding this comment.
@SilanHe Done in 62b71938: removed the redundant caller-client user-agent tests across callback_reporter_test.py, validation_test.py, agentcore_operation_test.py, transports_test.py, and llm_test.py.
| user_agent_extra=( | ||
| f"aws-durable-execution-sdk-python-extensions/{__version__} " | ||
| "md/agentcore" | ||
| ), |
There was a problem hiding this comment.
Nit: the same label text is typed out in 5 places.
Each default client builds "<package>/{__version__} md/<feature>" by hand. Two places are in compute-worker (callback_reporter.py L287-L290, self_suspend.py L147-L150). Three are in extensions (ai/_shared.py L138-L141, here, and compute/microvm/operation.py L1112-L1115).
What to change: one small helper per package, such as user_agent_extra(feature: str) -> str next to __about__, would remove the repeats. It would also make the AgentCore label fix in callback_reporter.py a one-word change.
🤖 AI-assisted comment
There was a problem hiding this comment.
@SilanHe Done in 62b71938: added _user_agent.user_agent_extra(feature: str) -> str next to __about__.py in both packages and replaced all 5 inline string constructions.
…kers # Conflicts: # packages/aws-durable-execution-sdk-python-extensions/docs/ai.md # packages/aws-durable-execution-sdk-python-extensions/tests/ai/llm_test.py
- Extract _user_agent.user_agent_extra in both packages - Add feature param to CallbackReporter.create for agentcore - Remove redundant caller-client user-agent tests - Update llm_test and agentcore_worker_test assertions
|
@SilanHe Thank you for the review! I've merged
Local verification on
Could you please re-review when you have a moment? |
Issue #, if available
Fixes #985
Twin: aws/aws-durable-execution-sdk-js#1281
Description of changes
Add
user_agent_extra(<package-name>/<version> md/<feature>) to every default boto3 client created byaws-durable-execution-sdk-python-extensionsandaws-durable-execution-sdk-python-compute-worker:extensions(compute/microvm/operation.py):aws-durable-execution-sdk-python-extensions/<version> md/microvmextensions(compute/agentcore/operation.py):aws-durable-execution-sdk-python-extensions/<version> md/agentcoreextensions(ai/_shared.pyLazyClientfor AgentCore, Bedrock Runtime, Lambda, and SageMaker Runtime):aws-durable-execution-sdk-python-extensions/<version> md/aicompute-worker(microvm/callback_reporter.py):aws-durable-execution-sdk-python-compute-worker/<version> md/microvmfor MicroVM workers, andaws-durable-execution-sdk-python-compute-worker/<version> md/agentcorewhen called withfeature="agentcore"fromagentcore/worker.pycompute-worker(microvm/self_suspend.py):aws-durable-execution-sdk-python-compute-worker/<version> md/microvmDocument across field docstrings and package markdown docs (
docs/microvm.md,docs/ai.md,docs/worker.md) that caller-provided clients (client,create_client,create_microvms_client) are used as they are and keep their own user agent.Design
See Rules for every change.
extensionsandcompute-workeromituser_agent_extra, so service logs cannot distinguish SDK-originated calls (RunMicrovm,InvokeAgentRuntime, model calls, callback APIs,SuspendMicrovm) from unrelated calls made inside Lambda functions.boto3.session.Session().client(...)orboto3.client(...)without settinguser_agent_extraonbotocore.config.Config, andagentcore/worker.pypreviously calledCallbackReporter.create(...)without distinguishing its feature label frommicrovm.f"<package>/{__version__} md/<feature>"in all 5 client factories — repeats the package-name and version format string across modules._user_agent.user_agent_extra(feature: str) -> strhelper next to__about__.pyin each package, and add an optional keyword-onlyfeature: str = "microvm"parameter to_create_clientandCallbackReporter.create._user_agent.user_agent_extra(feature)in each package and passfeature="agentcore"fromagentcore/worker.pyintoCallbackReporter.create. Each package builds<package-name>/<version> md/<feature>in one place without cross-package coupling, and custom caller clients remain untouched.user_agent_extrastrings across the 5 client factories.feature: str = "microvm"parameter was added onCallbackReporter.create/_create_client.+89/-22across code,_user_agent.pyhelpers, imports, and docstrings); 1 optional keyword-only parameter (feature: str = "microvm") added on publicCallbackReporter.create.Assumptions
Each assumption about the service, Lambda, the runtime or another library, with its evidence, or marked "unverified" (rule 6):
Config.user_agent_extraverbatim to the generatedUser-Agentheader inua/2.1format when constructing boto3 clients (verified locally withbotocore:<default ua> aws-durable-execution-sdk-python-extensions/<version> md/microvm).Decisions not covered by the issue or the design note
One line each, with the reason (rule 7):
__version__directly without_user_agent_version(+bundledrewriting) because only the core SDK receives+bundledruntime stamps (rule 4 avoids dead branches for unreachable versions).Checklist
68094c2added user-agent assertions for all default clients which failed onmainbefore theConfig(user_agent_extra=...)changes in commit 2.test_creates_the_default_client_on_first_use(packages/aws-durable-execution-sdk-python-extensions/tests/ai/transports_test.py): updated mockfake_client(service: str, *, config: Any = None)to accept the optionalconfigkeyword argument now passed byLazyClient.get().test_llm_defaults_to_the_bedrock_transport(packages/aws-durable-execution-sdk-python-extensions/tests/ai/llm_test.py): updated mockfake_client(service: str, **_kwargs: Any)to acceptconfig=passed byLazyClient.get().test_creates_its_own_reporter_without_create_client(packages/aws-durable-execution-sdk-python-compute-worker/tests/agentcore/agentcore_worker_test.py): updatedCallbackReporter.createmock signature to acceptfeature: str = "microvm"and assertfeature == "agentcore".test_the_given_client_is_used_and_no_client_is_createdandtest_reporter_closes_only_its_own_client(packages/aws-durable-execution-sdk-python-compute-worker/tests/microvm/callback_reporter_test.py): updated_create_clientmonkeypatches to accept*, feature: str = "microvm".test_creates_a_default_client_per_job_and_closes_it(packages/aws-durable-execution-sdk-python-compute-worker/tests/microvm/worker_test.py): updatedcreate_clientmonkeypatch to accept*, feature: str = "microvm"and assertfeature == "microvm".By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.