Skip to content

feat: mark extension and worker user agents (#985) - #992

Open
srivilliamsai wants to merge 5 commits into
aws:mainfrom
srivilliamsai:feat/user-agent-markers
Open

srivilliamsai wants to merge 5 commits into
aws:mainfrom
srivilliamsai:feat/user-agent-markers

Conversation

@srivilliamsai

@srivilliamsai srivilliamsai commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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 by aws-durable-execution-sdk-python-extensions and aws-durable-execution-sdk-python-compute-worker:

  • extensions (compute/microvm/operation.py): aws-durable-execution-sdk-python-extensions/<version> md/microvm
  • extensions (compute/agentcore/operation.py): aws-durable-execution-sdk-python-extensions/<version> md/agentcore
  • extensions (ai/_shared.py LazyClient for AgentCore, Bedrock Runtime, Lambda, and SageMaker Runtime): aws-durable-execution-sdk-python-extensions/<version> md/ai
  • compute-worker (microvm/callback_reporter.py): aws-durable-execution-sdk-python-compute-worker/<version> md/microvm for MicroVM workers, and aws-durable-execution-sdk-python-compute-worker/<version> md/agentcore when called with feature="agentcore" from agentcore/worker.py
  • compute-worker (microvm/self_suspend.py): aws-durable-execution-sdk-python-compute-worker/<version> md/microvm

Document 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.

  • Design note (rule 3), for a new feature, a new option or a public API change:
    1. Problem: Default boto3 clients in extensions and compute-worker omit user_agent_extra, so service logs cannot distinguish SDK-originated calls (RunMicrovm, InvokeAgentRuntime, model calls, callback APIs, SuspendMicrovm) from unrelated calls made inside Lambda functions.
    2. Cause: Each client factory instantiates boto3.session.Session().client(...) or boto3.client(...) without setting user_agent_extra on botocore.config.Config, and agentcore/worker.py previously called CallbackReporter.create(...) without distinguishing its feature label from microvm.
    3. Options considered:
      • Inline f"<package>/{__version__} md/<feature>" in all 5 client factories — repeats the package-name and version format string across modules.
      • Add one small package-private _user_agent.user_agent_extra(feature: str) -> str helper next to __about__.py in each package, and add an optional keyword-only feature: str = "microvm" parameter to _create_client and CallbackReporter.create.
    4. Chosen option and reason: Extract _user_agent.user_agent_extra(feature) in each package and pass feature="agentcore" from agentcore/worker.py into CallbackReporter.create. Each package builds <package-name>/<version> md/<feature> in one place without cross-package coupling, and custom caller clients remain untouched.
    5. What the change removes: Repeated inline user_agent_extra strings across the 5 client factories.
  • Simpler options considered (rule 2): No runtime configuration flags or public options were added for overriding default markers; only an optional keyword-only feature: str = "microvm" parameter was added on CallbackReporter.create / _create_client.
  • Root cause (rule 5): N/A (feature enhancement).
  • Net change in source lines and in public API members: +67 net lines across source files (+89/-22 across code, _user_agent.py helpers, imports, and docstrings); 1 optional keyword-only parameter (feature: str = "microvm") added on public CallbackReporter.create.

Assumptions

Each assumption about the service, Lambda, the runtime or another library, with its evidence, or marked "unverified" (rule 6):

  • botocore appends Config.user_agent_extra verbatim to the generated User-Agent header in ua/2.1 format when constructing boto3 clients (verified locally with botocore: <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):

  • Used __version__ directly without _user_agent_version (+bundled rewriting) because only the core SDK receives +bundled runtime stamps (rule 4 avoids dead branches for unreachable versions).

Checklist

  • Every new branch has a test that fails without it (rule 4)
  • For a bug fix: the first commit adds a test that fails on the current code (rule 5) - commit 68094c2 added user-agent assertions for all default clients which failed on main before the Config(user_agent_extra=...) changes in commit 2.
  • Every edited test expectation is listed in the description, with the reason (rule 5):
    • test_creates_the_default_client_on_first_use (packages/aws-durable-execution-sdk-python-extensions/tests/ai/transports_test.py): updated mock fake_client(service: str, *, config: Any = None) to accept the optional config keyword argument now passed by LazyClient.get().
    • test_llm_defaults_to_the_bedrock_transport (packages/aws-durable-execution-sdk-python-extensions/tests/ai/llm_test.py): updated mock fake_client(service: str, **_kwargs: Any) to accept config= passed by LazyClient.get().
    • test_creates_its_own_reporter_without_create_client (packages/aws-durable-execution-sdk-python-compute-worker/tests/agentcore/agentcore_worker_test.py): updated CallbackReporter.create mock signature to accept feature: str = "microvm" and assert feature == "agentcore".
    • test_the_given_client_is_used_and_no_client_is_created and test_reporter_closes_only_its_own_client (packages/aws-durable-execution-sdk-python-compute-worker/tests/microvm/callback_reporter_test.py): updated _create_client monkeypatches 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): updated create_client monkeypatch to accept *, feature: str = "microvm" and assert feature == "microvm".

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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.
@srivilliamsai
srivilliamsai deployed to ai-pr-review-runtime November 25, 2026 15:37 — with GitHub Actions Active
@SilanHe
SilanHe self-requested a review December 2, 2026 17:03
@SilanHe SilanHe self-assigned this Dec 2, 2026
Comment on lines +287 to +290
user_agent_extra=(
"aws-durable-execution-sdk-python-compute-worker/"
f"{__version__} md/microvm"
),

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

Comment on lines +252 to +297
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"

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@SilanHe Fixed in 54d1f7f0 / 62b71938: merged upstream/main and rewrote test_default_client_sets_user_agent on top of bedrock_transport().

Comment on lines +493 to +503
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"

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

Comment on lines +732 to +735
user_agent_extra=(
f"aws-durable-execution-sdk-python-extensions/{__version__} "
"md/agentcore"
),

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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
@srivilliamsai

Copy link
Copy Markdown
Contributor Author

@SilanHe Thank you for the review! I've merged upstream/main (e825bbb5 in 54d1f7f0) and pushed all requested fixes in 62b71938:

  1. AgentCore worker label (md/agentcore): Added *, feature: str = "microvm" to _create_client and CallbackReporter.create in microvm/callback_reporter.py, updated agentcore/worker.py to pass feature="agentcore", and updated test_creates_its_own_reporter_without_create_client (agentcore_worker_test.py), callback_reporter_test.py, and worker_test.py to assert the feature label.
  2. llm_test.py aligned with main: Resolved the merge with upstream/main (#1004), rewrote test_default_client_sets_user_agent on top of bedrock_transport(), and updated fake_client in test_llm_defaults_to_the_bedrock_transport to accept **_kwargs: Any when boto3.client(...) receives config=.
  3. Removed redundant passed-client tests: Dropped the test_passed_*_client_keeps_its_user_agent tests from callback_reporter_test.py, validation_test.py, agentcore_operation_test.py, transports_test.py, and llm_test.py (identity and non-creation are already verified by the existing tests).
  4. Shared user_agent_extra helper: Extracted user_agent_extra(feature: str) -> str into _user_agent.py (next to __about__.py) in both aws-durable-execution-sdk-python-extensions and aws-durable-execution-sdk-python-compute-worker, and wired it into all 5 default client factories.

Local verification on 62b71938:

  • hatch fmt --check (extensions & compute-worker): passed
  • hatch run dev-extensions:typecheck & hatch run dev-compute-worker:typecheck: passed (0 issues)
  • hatch run dev-extensions:cov: 647 passed (99% coverage)
  • hatch run dev-compute-worker:cov: 513 passed (99% coverage)
  • python3 .github/scripts/lintcommit.py --range "main..HEAD": passed

Could you please re-review when you have a moment?

This branch was successfully deployed

1 active (outdated) deployment
ai-pr-review-runtime — 39179164 Deployed Nov 25, 2026 by srivilliamsai via notify / Generate notification summary #502
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mark extensions and compute-worker AWS calls in the user agent

2 participants