fix(invoke): auto-generate session ID for bearer-token invocations#953
Open
aidandaly24 wants to merge 1 commit intoaws:mainfrom
Open
fix(invoke): auto-generate session ID for bearer-token invocations#953aidandaly24 wants to merge 1 commit intoaws:mainfrom
aidandaly24 wants to merge 1 commit intoaws:mainfrom
Conversation
Closes aws#840 When invoking an agent with a bearer token (OAuth/CUSTOM_JWT) and no session ID, `AgentCoreMemoryConfig` raised a Pydantic validation error because `session_id=None` is rejected. Unlike SigV4 callers, bearer-token callers do not get a server-side auto-generated runtime session ID. Two-layer fix: 1. CLI synthesizes a UUID in `invoke` action when `--bearer-token` is set and `--session-id` is missing, using the existing `generateSessionId` helper. Covers both explicit `--bearer-token` and the CUSTOM_JWT auto-fetch path. 2. Strands memory session templates (http, agui, a2a) synthesize a UUID when `session_id` is falsy before constructing AgentCoreMemoryConfig. Protects direct runtime callers (curl, custom apps) who forget the `X-Amzn-Bedrock-AgentCore-Runtime-Session-Id` header. Snapshot tests updated.
Contributor
Package TarballHow to installnpm install https://github.com/aws/agentcore-cli/releases/download/pr-953-tarball/aws-agentcore-0.10.0.tgz |
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.
Description
When invoking an agent with a bearer token (OAuth/CUSTOM_JWT) and no session ID,
AgentCoreMemoryConfigraised a Pydantic validation error becausesession_id=Noneis rejected:Unlike SigV4 callers, bearer-token callers do not get a server-side auto-generated runtime session ID from AgentCore, so the
Nonevalue reached the memory-enabled Strands templates and broke them.Two-layer fix
CLI auto-generation (
src/cli/commands/invoke/action.ts) — whenoptions.bearerTokenis set andoptions.sessionIdis missing, synthesize a UUID via the existinggenerateSessionIdhelper before dispatching to any invocation branch (streaming, non-streaming, MCP, A2A, AGUI, exec). Covers both explicit--bearer-tokenand the CUSTOM_JWT auto-fetch path.Template hardening (
src/assets/python/{http,agui,a2a}/strands/capabilities/memory/session.py) — each template now importsuuid, widens the parameter type toOptional[str], and synthesizessession_id = session_id or uuid.uuid4().hexbefore constructingAgentCoreMemoryConfig. This protects non-CLI callers (curl, Postman, custom apps) who reach the deployed runtime without a session header.Related Issue
Closes #840
Documentation PR
N/A
Type of Change
Testing
Unit + snapshot tests:
npm run test:unitandnpm run test:integnpm run typechecknpm run lintsrc/assets/, I rannpm run test:update-snapshotsand committed the updated snapshotsEnd-to-end verification against a deployed agent
Deployed a Strands agent with CUSTOM_JWT (Cognito M2M) inbound auth + short-term memory to
us-west-2, built the CLI from this PR's HEAD, and exercised both call paths:agentcore invoke --bearer-token $TOKEN "What is 2+2?"with no--session-id→"2 + 2 = **4**"; invoke log confirms a UUID session ID was synthesized client-side.curlto the runtime/invocationsendpoint without anX-Amzn-Bedrock-AgentCore-Runtime-Session-Idheader → agent responded normally; the Python template self-defended viauuid.uuid4().hex.Full command output and session ID evidence: https://gist.github.com/aidandaly24/d8fb51e4048acd0dd2e6f670c701b857
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.